Solved

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

Posted on 2014-09-06
6
558 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hide vba in gp 7 82
how to use laptop or pad camera in vb.net windows application 2 49
SQL syntax in VB.net 5 28
fso.FolderExists("\\server\HiddenFolder$") 4 49
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

911 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

21 Experts available now in Live!

Get 1:1 Help Now