By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

Object Reference not set.......

P: n/a
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

Jan 24 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

<vi****@visrez.comwrote in message
news:11*********************@d71g2000cwa.googlegro ups.com...
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

Jan 24 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.