Solved

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

Posted on 2004-10-13
9
1,716 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 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

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!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 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