Object Reference not set.......

V

vishnu

Hi,
I am working on asp.net project which I converted the code fron VB
to C# and instead of RaiseEvent in VB code I used the following code.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.IO;
using Toolkit;


public partial class Controls_SelectImage : System.Web.UI.UserControl
{
public delegate void ImageFinalizedEventHandler(object sender,
FileEventArgs e);
public event ImageFinalizedEventHandler ImageFinalized;

protected void Page_Load(object sender, EventArgs e)
{
// Sync the literal in the instructions with the button's text
litFinishButtonText.Text = btnFinish.Text;
}


protected void btnFinish_Click(object sender, EventArgs e)
{

//////RaiseBubbleEvent
try
{
////ImageFinalizedEventHandler eventhandler =
ImageFinalized;
////if (eventhandler != null)
// if (this.ImageFinalized != null)
//ImageFinalized += new ImageFinalizedEventHandler(
OnImageFinalized( new FileEventArgs(this.FileName));


}
catch (Exception ex)
{
throw new Exception(ex.Message);
}

//RaiseEvent ImageFinalized(Me, New
FileHandlingEventArgs(FileName));

}

protected void OnImageFinalized(FileEventArgs e)
{
if (ImageFinalized != null)
ImageFinalized(this, e);
}
}

Since ImageFinalized is Null it is not executing ImageFinalized and if
I remove the statement "if (ImageFinalized != null)" It is showing the
error "Object Reference not set......"

More over FileEventArgs is a class,the class code is :


public class FileEventArgs : EventArgs
{

private string _FileName;


//public FileEventArgs()
//{ }

public FileEventArgs(string theFileName):base()
{
_FileName = theFileName;
}

public string FileName
{
get
{
return _FileName;
}
set
{
_FileName = value;
}
}
}

I know that since ImageFinalized method is not yet initialized its
throwing exception.

Can any one help me out to resolve this issue



Regards
Vishnu
 
K

Kevin Spencer

ImageFinalized is an event, which means essentially that it is a declaration
of an event delegate (in this case, ImageFinalizedEventHandler). A delegate
is a special type of class which represents or is a placeholder for one or
more methods. This way, one or more methods can be invoked by assigning them
to an event delegate. So, until you assign a delegate method to an event,
the reference is null.

In C#, delegates are assigned to events by using the addition (+) operator.
Since a delegate (also known as a "Multicast delegate," because it can
actually represent more than one delegate method) can have more than one
method assigned to it, the (+=) operator is most commonly used. In C#, (+=)
is the equivalent of (instance = instance + instance). Instead, it is
written in shorthand (instance += instance).

So, until an EventHandler method of the correct type is assigned to an
event, the reference is null, and an attempt to call it as a method will
throw an "object reference not set to an instance of an object" Exception.

It's a little confusing at first, but once you understand it fully, it makes
sense, and you can find many opportunities to use delegates, not just for
event-handling, because a delegate is a placeholder for one or more methods.
delegates are "Multicast" so that, for example, there can be any number of
"client" objects that subscribe to a single event. They are internally a
linked list of pointers to methods, and are invoked one at a time, in the
order in which they were assigned to the delegate variable, all with a
single call to the placeholder.

So, not only are delegates useful for events, but for any situation in which
you might want to call an as-yet-unspecified method in a class. The only
requirement is that the method signature matches the signature of the
delegate declaration.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

In case of Minimalism, break Philip Glass.
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top