|
-
Apr 25th, 2011, 04:22 PM
#1
Thread Starter
Hyperactive Member
[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?
-
Apr 25th, 2011, 04:27 PM
#2
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)
-
Apr 25th, 2011, 04:44 PM
#3
Thread Starter
Hyperactive Member
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)
-
Apr 25th, 2011, 04:47 PM
#4
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)
-
Apr 26th, 2011, 08:23 AM
#5
Thread Starter
Hyperactive Member
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.
-
Apr 27th, 2011, 06:25 PM
#6
Re: Object reference not set on event
 Originally Posted by Cyb3rH4Xter
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|