Results 1 to 6 of 6

Thread: [RESOLVED] Object reference not set on event

  1. #1

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Resolved [RESOLVED] Object reference not set on event

    I get an object reference not set error on this line:

    Code:
    AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnApplicationExit);
    The method OnApplicationExit is a public void non-static. What should I do to get rid of this error beside making it static?

  2. #2
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Object reference not set on event

    Change its name. OnApplicationExit is ambiguous because an event already exists by that name. You'll probably be OK once you change it. (Just use find/replace.)
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  3. #3

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Object reference not set on event

    Hmm didn't work, even tried YellowHoneyBunny.

    Here is the exact message:

    Code:
    Error	1	An object reference is required for the non-static field, method, or property 'Client.Program.Process_Exit(object, System.EventArgs)'
    Here is the code:

    Code:
            static void Main(string[] args)
            {
                AppDomain.CurrentDomain.ProcessExit += new EventHandler(Process_Exit);
            }
    
            // The event handler for application exit
            public void Process_Exit(object sender, EventArgs e)
            {
               stuff
            }
    Maybe I am declaring it in the wrong place somehow? (It's a console app btw)

  4. #4
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Object reference not set on event

    Oh you're doing this in a console app! Since your main thread runs as static, your event handlers that are a part of that same class need to also be static.

    If you want it to be non-static, create a new class somewhere, declare an instance of that class and handle the event in that instance.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  5. #5

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Object reference not set on event

    I first created a new class and so on and got it nearly working until I realized it was better to make it all static.

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Object reference not set on event

    Quote Originally Posted by Cyb3rH4Xter View Post
    I first created a new class and so on and got it nearly working until I realized it was better to make it all static.
    No, no it really wasn't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width