Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Debug me...
Posted

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.

Debug me...
  • Administrators
Posted

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.

if (myEvent != null){
this.myEvent(this, e);}

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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

Nothing is as illusive as 'the last bug'.
Posted
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 ...

Debug me...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...