0

Frustrated have been looking at this all day... I am creating a windows service...this code works fine on a regular console application and does not work on the windows service. I am getting a null reference exception:

Null reference in OnStart(): Object reference not set to an instance of an object. 
 at VNurseService.Server.RequestServer..ctor(Server s)
 at VNurseService.VNurseServer.OnStart(String[] args)

Code looks like:

protected override void OnStart(string[] args)
    {

        eventLog1.WriteEntry("In OnStart");
       Start:
        try
        {
            server = new Server();

            Server.RequestServer reference = new Server.RequestServer(server);
            new Thread(reference.run).Start();
            //r1 = new Thread(reference.run); r1.Start();

            Server.ResponseServer reference2 = new Server.ResponseServer(server);
            new Thread(reference2.run).Start();
            //r2 = new Thread(reference.run); r2.Start();

            Server.reference3 = new Server.ConfirmationServer(server);

            server.guiServer = new Guicom();
            server.restartServer = new Restart();

            eventLog1.WriteEntry("Restart in server is " + server.RESTART.ToString());

            new Thread(server.guiServer.accept).Start();
            new Thread(server.restartServer.accept).Start();
            goto check;

        check:
            while (true)
            {
                eventLog1.WriteEntry("CheckRestart is " + server.checkRestart().ToString());
                if (server.checkRestart())
                {
                    reference.cleanup();
                    //r1.Abort();
                    reference2.cleanup();
                    //r2.Abort();
                    //server = null;
                    goto Start;
                }
            }
        }
        catch (NullReferenceException ex)
        {
            eventLog1.WriteEntry("Null reference in OnStart(): " + ex.Message+ " " + ex.InnerException + @"\n " + ex.StackTrace);
        }
    }

And something is giving me a null reference exception and I don't know where it is.

Thanks for the help in advance.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121

3 Answers3

5

You can add a forced breakpoint in code at service startup ...

protected override void OnStart(string[] args)     
{   
    System.Diagnostics.Debugger.Break();       

    eventLog1.WriteEntry("In OnStart"); 
    ...
}

.. and attach a debugger to the process

Simen S
  • 3,210
  • 18
  • 24
  • 1
    Additional reading recommendations [How to: Debug Windows Service Applications](http://msdn.microsoft.com/en-us/library/7a50syb3.aspx) and [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – R. Martinho Fernandes Mar 10 '11 at 23:14
1
 eventLog1.WriteEntry("Null reference in OnStart(): " + ...);

I think your are a victim of your own message. The exception didn't actually occur in OnStart, even though your message says it did. It bombed in the RequestServer class, code you didn't post. Note how the call stack does give you the right info. The .ctor() listed there is the constructor for the class, that's the one that bombed.

Have a look at it. And fix the WriteEntry() argument, I'd recommend something like "Startup failure".

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

Better still, launch a debugger:

protected override void OnStart( string [] args )
{
    if ( args.Contains( "debug" ) ) System.Diagnostics.Debugger.Launch();
}

In services.msc, you can add parameters to be passed on startup by right clicking on the (stopped) service, "Properties/General/Start Parameters"

simonoch
  • 31
  • 2