Solved

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

Posted on 2014-09-06
6
566 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 63

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
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.

 
LVL 63

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 63

Expert Comment

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

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

827 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