Can't solve NullReferenceException

Malfunction

Junior Contributor
Joined
Dec 8, 2003
Messages
203
Location
Berlin, Germany
I'm getting a System.NullReferenceException in the following line of code:
this.myEvent(this, e);
In the debugger window "this" and "e" are not null in fact they are what they're supposed to be: this is of the type myControl and e is of the typeMouseEventArgs - a messageBox before that line of code confirms that.

I even removed the parameters from the delegate but
this.myEvent(); throws the same exception.

I checked every field in the debugger window and still couldn't find the null reference.
 
How can the event cause an exception?
So far there's no event handler catching this event and the debugger always points to that line of code.
It's driving me nuts because I can't find a null pointer by the time the exception is thrown.
 
What is this.MyEvent? Could you show where this is declared. If it is a delegate is it assigned anything?

Normally when invoking an event handler you need to check if it points to anything.
C#:
if (myEvent != null){
this.myEvent(this, e);}
 
If nobody subscribed to your event yet (they have to do MyEvent += <delegate_to_eventhandler>), the this.MyEvent object is still null (which is a very annoying feature IMHO).

Use the construction above by PlausiblyDamp to handle such a situation.

Edit: typo
 
Wile said:
If nobody subscribed to your event yet (they have to do MyEvent += <delegate_to_eventhandler>), the this.MyEvent object is still null (which is a very annoying feature IMHO).

Use the construction above by PlausiblyDamp to handle such a situation.

Edit: typo

thx I really didn't know that ...
 
Back
Top