Solved

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

Posted on 2004-10-13
9
1,669 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 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