Solved

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

Posted on 2014-09-06
6
567 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
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 …

749 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