473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Object Reference not set.......

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
1 2729
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Chris S. | last post by:
I'm trying to make a graphical editor and browser for Pickled files. One aspect I'm not sure about is how to detect multiple references to the same data. For instance, say I had the Pickled...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
29
by: web1110 | last post by:
If I have 2 variables, A and B, referencing the same object and then do a A.Dispose(), what happens to B?
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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,...

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.