Solved

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

Posted on 2004-10-13
9
1,695 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

789 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