Solved

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

Posted on 2014-09-06
6
551 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

759 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

20 Experts available now in Live!

Get 1:1 Help Now