Null Reference Exception [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-13 05:44:05

问题


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.


回答1:


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




回答2:


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




回答3:


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"



来源:https://stackoverflow.com/questions/5266971/null-reference-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!