EventArgs raise a NullReferenceException

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Why do I get a System.NullReferenceException for this script?
I added the following script to a simple form with a button.
<Script>
public event EventHandler DoThis;
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
OnDoThis();
}
protected void OnDoThis()
{
//The DoThis EventArgs is not recognize at run time
//This and new EventArgs() are fine.
DoThis(this, new EventArgs());
}
</Script>

Thank you
 
Hi ZAky,

This is not EventArgs that thorws the exception.

Your event is not hooked up. In this case the deleagte class member that
holds the chain of event hadlers is not initialized and is null. So, using
that not initilized reference is what cause the exception
....
DoThis(this, new EventArgs());
//DoThis is null
....
 
Hi ZAky

Don call the delegate if it is null.
if(DoThis != null)
DoThis(this, new EventArgs());

Anyways I suggesto to stick with the design pattern microsoft uses
throughout WindowsForms framework
//Class for event releated data
public class SomethingHappenEventArgs: EventArgs
{
.....
//Event data goes here
}

//Delegate for the event
public delegate void SomethingHappenEventHandler(object sender,
SomeEventEventArgs e);

class Foo
{
public SomethingHappenEventHandler SomethingHappen;

protected virtual OnSomethingHappen(SomethingHappenEventArgs e)
{
if(SomethingHappen != null)
SomethingHappen(this, e);
}
}

class inheritors can handle the event by overriding the OnXXX method and at
the same time block the event from firing. Other methods can raise the event
by calling the OnXXX method.

There is an article of best practices for declaring events in MSDN


--
HTH
Stoitcho Goutsev (100) [C# MVP]


ZAky said:
What should I do?
I don't need the class that raise the event to hook the event.
I want classes that use this class to hook the event.

Thank you
ZAky

Stoitcho Goutsev (100) said:
Hi ZAky,

This is not EventArgs that thorws the exception.

Your event is not hooked up. In this case the deleagte class member that
holds the chain of event hadlers is not initialized and is null. So, using
that not initilized reference is what cause the exception
....
DoThis(this, new EventArgs());
//DoThis is null
....
--
HTH
Stoitcho Goutsev (100) [C# MVP]


ZAky said:
Why do I get a System.NullReferenceException for this script?
I added the following script to a simple form with a button.
<Script>
public event EventHandler DoThis;
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
OnDoThis();
}
protected void OnDoThis()
{
//The DoThis EventArgs is not recognize at run time
//This and new EventArgs() are fine.
DoThis(this, new EventArgs());
}
</Script>

Thank you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top