|
-
Oct 30th, 2010, 04:29 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Custom Event Returned Null..?
I am a bit confused on this particular error I am getting. Usually when I get an error, I know the area the error occurred and I understand what could have lead up to my error. Today, I just got an error on a custom event I wrote.
What I don't understand is that I copied this code from another one of my projects were it worked perfectly. Also, this error does not make sense.. How can a method being called, result in a Null Reference error? I am not handling anything returned by the call, I am simply making the call, both parameters being passed have a value - I checked.
Here is the error: (There is no reason to get the innerException or any more detailed error reports as they aren't really more detailed they simply give the same error.)

Basically, I'm calling a custom event from a class I wrote...
If the information provided isn't enough, what do you think the possible causes could be? What could be the factors that might contribute to this error so I can go back and take a look that them?
-
Oct 30th, 2010, 05:00 PM
#2
Re: Custom Event Returned Null..?
Don't ask me why, but according to an MSDN example you should check whether the NewOutputEvent is null.
 Originally Posted by MSDN
protected virtual void OnAlarm(AlarmEventArgs e)
{
if (Alarm != null)
{
//Invokes the delegates.
Alarm(this, e);
}
}
}
http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx
Delete it. They just clutter threads anyway.
-
Oct 30th, 2010, 05:05 PM
#3
Thread Starter
Frenzied Member
Re: Custom Event Returned Null..?
I think I am going to read the link provided in the article about the Delegates.
Thank you, even though this is not the solution I was looking for it will do.
-
Oct 30th, 2010, 08:18 PM
#4
Re: [RESOLVED] Custom Event Returned Null..?
You can think of an event as a field or property that can refer to a delegate. By default, that field or property is null. When you add a handler for that event, you are creating a delegate object and assigning it to the field or property.
Now, when you raise an event you are basically calling the Invoke method of the delegate assgned to the event. If you haven't assigned a delegate, i.e. added a handler, then the event is null. What happens when you try to call a method of an object using a variable that is null? You get a NullReferenceException, right? That's exactly what's happening here.
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
|