Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Raising an Event -- Object reference not set to an instance of an object

Posted on 2003-11-06
6
Medium Priority
?
2,741 Views
Last Modified: 2012-05-04
Hello,
I'm getting the error:
System.NullReferenceException: Object reference not set to an instance of an object.
The code is below, and the error is on the line that I've indicated with arrows.
Why is this? I can't see what I'm doing wrong. Is this not how you raise an event?
I practically copied this example http://www.akadia.com/services/dotnet_user_controls.html.
Please help!
Thank you.

//this is my web form
namespace TestWebApp
{
  public class MyWebForm : System.Web.UI.Page
  {
    protected MyControlNS.MyControl MyControl;
    private void Page_Load(object sender, System.EventArgs e)
    {
      MyControl = new MyControlNS.MyControl();
      MyControl.TestEvent();
    }
  }
}

//This is my web control
namespace MyControlNS
{
  public class MyControl : System.Web.UI.WebControls.WebControl
  {
    public delegate void EventHandler(Object sender, EventArgs e);

    public event EventHandler MyEvent;

    public int TestEvent()
    {
      MyEvent(this, new System.EventArgs()); // <---- <---- <----
      return 1;
    }
  }
}
0
Comment
Question by:jini555
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:Tonylmiller
ID: 9698128
I normally do it something like this:

this.TextBox1.Leave += new System.EventHandler(this.TextBox2_Leave);

private void maskedTextBox2_Leave(object sender, System.EventArgs e)
{
      //Your code here!
}

0
 
LVL 9

Accepted Solution

by:
tinchos earned 560 total points
ID: 9698591
Hi jini555,

The problem is the following .......

you can raise an event, only when it has a handler associated to it, otherwise, as you said, it will throw the  
"Object reference not set to an instance of an object" exception.

In order to avoid this, you can do the following

if( MyControl.TestEvent != null )
     MyControl.TestEvent();

in this way, you'll first avoid the case when testevent is null (does not have any handler associated).
Besides, what's the use of throw an event if it does not have any handler associated to it.

and will be thrown when it has someone who will listen to it.


Hope this helps!

Tincho
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9698612
Just a quote from Programming C# 2nd Edition (O'Reilly)

Example:


It then notifies the subscribers by firing the OnSecondChange event:

// if anyone has subscribed, notify them
if (OnSecondChange != null)
{
     OnSecondChange(this,timeInformation);
}

If an event has no subscribers registered, it will evaluate to null. The test above checks that
the value is not null, ensuring that there are subscribers before calling OnSecondChange.

Cheers

Tincho
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:jini555
ID: 9699370
tinchos,
thank you VERY MUCH for taking the time to explain clearly. I am at home right now, but when I get into work tomorrow, I will make that small change.
I'll get back to you afterwards.

tonylmiller,
Thank you for your input as well. I wanted to merely understand the bug in my code rather than do it another way. :)
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9700637
Ok,

hope that solves all you need.

Tincho
0
 

Author Comment

by:jini555
ID: 9718694
tincho,
sorry for the delay, something came up and i was away.

anyways, yes, that error is gone now :).


i should note, however, that i could not compile it without removing the "event" keyword. According to this article I just found, (which i think might be the one you quoted)

http://www.ondotnet.com/pub/a/dotnet/2003/06/23/progCsharp3.html?page=3

the "event" keyword prevents another class from accessing the delegate with an operator other than += and -= . So -- I will check for null *inside* my control, rather than from the web form.

Thank you,
-J
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question