Solved

Object reference not set to an instance of an object inside Windows Service

Posted on 2014-09-06
6
562 Views
Last Modified: 2014-09-07
I'm developing a VB Windows Service than start a process and finally kill it. In a moment it was working fine but, trying to clean some code, it seems that I've missed something.

Now I'm getting the following error: "Object reference not set to an instance of an object", here

    Private Sub StartExes()
        Dim Idx As Integer = 0
        Try
            LoadINIfile()
            Dim myProccess As New Process
            For Idx = 0 To 4
                If exeNames(Idx, 0).Length > 0 Then
                    myProccess.StartInfo.FileName = exePath & exeNames(Idx, 1)
                    myProccess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
                    myProccess.Start()
                End If
            Next
        Catch ex As Exception
            Dim rcbool As Boolean = TraceActivity("SERVICE", "StartExes: " & ex.Message)
            Dim oSvcController As New ServiceController("CRSSERVICE")
            oSvcController.Stop()

        End Try
    End Sub

Any idea?
0
Comment
Question by:leorocha
  • 3
  • 3
6 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40307913
Where are the following two variables defined, exeNames and exePath? Also please show the line where the exception happens on.
0
 

Author Comment

by:leorocha
ID: 40307922
In order to debug the code i've commented this piece of code that executes once using a timer created and enabled at OnStart:
        Dim Idx As Integer = 0
        Dim myProccess As Process
        Try
            '            LoadINIfile()
            '            myProccess = New Process
            '            For Idx = 0 To 4
            '            If exeNames(Idx, 0).Length > 0 Then
            '            myProccess.StartInfo.FileName = exePath & exeNames(Idx, 1)
            '            myProccess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
            '            myProccess.Start()
            '            End If
            '            Next
        Catch ex As Exception
            Dim rcbool As Boolean = TraceActivity("SERVICE", "StartExes: " & ex.Message)
            Dim oSvcController As New ServiceController("CRSSERVICE")
            oSvcController.Stop()

        End Try

This way I can debug the code from a SUB executed at OnStop:
    Private Sub KillExes()
        Dim Idx As Integer = 0
        Try
            Dim rcbool As Boolean = TraceActivity("SERVICE", "Deteniendo el SERVICE")
            For Idx = 0 To 4
                If exeNames(Idx, 0).Length > 0 Then
                    Dim myProcesses() As Process =  Process.GetProcessesByName(exeNames(Idx, 0))
                    Dim myProccess As Process
                    For Each myProccess In myProcesses
                        myProccess.Kill()
                        myProccess.WaitForExit()
                    Next
                End If
            Next

        Catch ex As Exception
            Dim rcbool As Boolean = TraceActivity("SERVICE", "KillExes: " & ex.Message)
            Dim oSvcController As New ServiceController("CRSSERVICE")
            oSvcController.Stop()
        End Try
    End Sub

The error ocurrs here:
                    Dim myProcesses() As Process =  Process.GetProcessesByName(exeNames(Idx, 0))

Variables definition:

Imports System.IO
Imports System.ServiceProcess
Imports System.Diagnostics
Imports ALT_IniFile
Public Class CRSServ
    Private Shared timer As System.Timers.Timer
    Private Shared timerInterval As Integer
    Const TIMER_INTERVAL As Long = 1000
    Dim isRunning As Boolean
    Dim exeNames(5, 2) As String
    Dim exePath As String
0
 

Author Comment

by:leorocha
ID: 40307945
Ups, found!     "Dim exeNames(5, 2) As String"  ,this array was uninitialized.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40308161
Hi leorocha;

Yes, although this statement creates a two dimensional array of string all the elements are set to Nothing and the reason for the error "Object reference not set to an instance of an object" when you attempted to assign values to it.
0
 

Author Closing Comment

by:leorocha
ID: 40308220
You meke me think, jeje
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40308437
Well I am glad I made you think it out. Have a good day.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now