Results 1 to 4 of 4

Thread: [RESOLVED] Custom Event Returned Null..?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Resolved [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?

  2. #2
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Custom Event Returned Null..?

    Don't ask me why, but according to an MSDN example you should check whether the NewOutputEvent is null.
    Quote 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.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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