Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-13
9
Medium Priority
?
1,884 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
[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
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
Technology Partners: 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 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 1500 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

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!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

618 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