473,473 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

EventArgs raise a NullReferenceException

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
Nov 16 '05 #1
2 2539
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" <ZA**@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
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

Nov 16 '05 #2
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" <ZA**@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
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) [C# MVP]" wrote:
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" <ZA**@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
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


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fabio Papa | last post by:
Hi, I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :) I am writing a...
1
by: Diotima | last post by:
I'm raising an event from a C# component and trying to catch it in a VB.NET Windows application. The VB app recognizes my custom EventArgs object and catches the event. However, when I try to read...
4
by: Steve Amey | last post by:
Hi all I am creating a basic control to perform some tasks, and I want to declare some events to be raised so they can be handled from the form that the control is on. I can create my own Event...
5
by: TT (Tom Tempelaere) | last post by:
Hi, Once in a while my application throws an NullReferenceException at startup, however it appears to be occurring in an unknown module. If I debug it and ask to 'break', then there is no source...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
12
by: Benny Raymond | last post by:
I understand that you would normally want to raise an event on the same thread so that your code blocks until the event has been dealt with... however i've run into a situation where I have a...
11
by: nadeem_far | last post by:
Hello, I am working on a c# project using framework 1.1. Since its a console application,no windows forms are being used. I am trying to implement event driven classes that just want to raise...
0
by: Andrus | last post by:
Who is going to raise the events? Events are raised by DataGridView overridden OnCellValidated method. However code uses row entity as sender because: 1. Code can use sender. without casting...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.