Solved

System.NullReferenceException: Object reference not set to an instance of an object

Posted on 2004-10-13
9
1,616 Views
Last Modified: 2010-05-19
I have a .Net windows service written in VB.Net that runs on all of my test machines, yet when I go to run it on a client production machine, I get this error:

Service cannot be started. System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)
   at MyService.Service1.OnStart(String[] args)
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state).

The service runs under an account that has Admin access on the client machine.

My OnStart event is as follows:

Protected Overrides Sub OnStart(ByVal args() As String)
        ' Get the Log_Path from the registry
        Dim objWSHRead_Shell
        Const LogPathKey = "HKEY_LOCAL_MACHINE\Software\InfoGenesis\PBS\LogReader_Client\"
        objWSHRead_Shell = CreateObject("WScript.Shell")
        LogPath = objWSHRead_Shell.RegRead(LogPathKey & "Log_Path")
        objWSHRead_Shell = Nothing

        ' Log the start time of the service
        Dim objWriter As System.IO.StreamWriter
        Try
            Dim LogFileName As String
            Dim LogMessage As String

            LogFileName = Now.Month & Now.Day & Now.Year & ".txt"
            LogMessage = Now.Now & " - Client Service was started"

            objWriter = System.IO.File.AppendText(LogPath & LogFileName)
            objWriter.WriteLine(LogMessage)
            objWriter.Close()
        Catch
        End Try

        Call ProcessFiles()
    End Sub
0
Comment
Question by:ABaruh
9 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12302404
Use the NEW keyword.

        Dim objWriter As New System.IO.StreamWriter
-Baan
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12302426
and pass the file path as argument.
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12302455

Protected Overrides Sub OnStart(ByVal args() As String)
        ' Get the Log_Path from the registry
        Dim objWSHRead_Shell
        Const LogPathKey = "HKEY_LOCAL_MACHINE\Software\InfoGenesis\PBS\LogReader_Client\"
        objWSHRead_Shell = CreateObject("WScript.Shell")
        LogPath = objWSHRead_Shell.RegRead(LogPathKey & "Log_Path")
        objWSHRead_Shell = Nothing

        ' Log the start time of the service
        Dim objWriter As New System.IO.StreamWriter(LogPath & LogFileName,True)
        Try
            Dim LogFileName As String
            Dim LogMessage As String

            LogFileName = Now.Month & Now.Day & Now.Year & ".txt"
            LogMessage = Now.Now & " - Client Service was started"

            objWriter.WriteLine(LogMessage)
            objWriter.Close()
        Catch
        End Try

        Call ProcessFiles()
    End Sub
0
 
LVL 2

Expert Comment

by:UncleMidriff
ID: 12302777
Your code looks good to me, DotNetLover, but I think you may have mixed up the declaring of LogFileName and its use.  In other words, I think it should be:

     ' Log the start time of the service
       
        Dim LogFileName As String
        Dim LogMessage As String
       
        Try

            LogFileName = Now.Month & Now.Day & Now.Year & ".txt"
            LogMessage = Now.Now & " - Client Service was started"

            Dim objWriter As New System.IO.StreamWriter(LogPath & LogFileName,True)
            objWriter.WriteLine(LogMessage)
            objWriter.Close()

        Catch

        End Try

Just a minor correction though.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Author Comment

by:ABaruh
ID: 12303018
I tried those solutions, it still won't start (again, works fine on all my test machines).

How can I get more details about why it won't start.  The error won't even tell me what line it errors on.  Can you recommend how to implement a debugger?
0
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 500 total points
ID: 12303453
May be this line is giving u error.. "  objWSHRead_Shell = CreateObject("WScript.Shell") "
Use try - catch for the whole sub.
-Baan
0
 
LVL 4

Expert Comment

by:andrewharris
ID: 12304187
Does the registry entry exist?

Andrew
0
 
LVL 7

Author Comment

by:ABaruh
ID: 12304495
The Reg Entry exists, as I said it works fine on test machines but not this one prod machine.

I have discovered the issue:

Rather than use
Dim objWSHRead_Shell
Const LogPathKey = "HKEY_LOCAL_MACHINE\Software\InfoGenesis\PBS\LogReader_Client\"
objWSHRead_Shell = CreateObject("WScript.Shell")
LogPath = objWSHRead_Shell.RegRead(LogPathKey & "Log_Path")
objWSHRead_Shell = Nothing

I had to change to an actual .Net Registry reading class:
Dim RegReader As RegistryKey
        Dim KeyValue As String
        KeyValue = "Software\InfoGenesis\PBS\LogReader_Client\"
        RegReader = Registry.LocalMachine.OpenSubKey(KeyValue, False)
        If (Not RegReader Is Nothing) Then
            LogPath = RegReader.GetValue("Log_Path", 0)
            RegReader.Close()
        End If

I guess on this one machine, there was something that did not like me using wscript.  Points go to DotNetLover_Bean
0
 
LVL 7

Author Comment

by:ABaruh
ID: 12304503
I would give DotNetLover_Bean an A but no solution was offered, just an indication of how to help isolate the prob
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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…

757 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

23 Experts available now in Live!

Get 1:1 Help Now