473,545 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Configur ation;
using System.Collecti ons;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Text.Reg ularExpressions ;
using System.IO;
using Toolkit;
public partial class Controls_Select Image : System.Web.UI.U serControl
{
public delegate void ImageFinalizedE ventHandler(obj ect sender,
FileEventArgs e);
public event ImageFinalizedE ventHandler ImageFinalized;

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

//////RaiseBubbleEven t
try
{
////ImageFinalizedE ventHandler eventhandler =
ImageFinalized;
////if (eventhandler != null)
// if (this.ImageFina lized != null)
//ImageFinalized += new ImageFinalizedE ventHandler(
OnImageFinalize d( new FileEventArgs(t his.FileName));
}
catch (Exception ex)
{
throw new Exception(ex.Me ssage);
}

//RaiseEvent ImageFinalized( Me, New
FileHandlingEve ntArgs(FileName ));

}

protected void OnImageFinalize d(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(s tring theFileName):ba se()
{
_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 2737
ImageFinalized is an event, which means essentially that it is a declaration
of an event delegate (in this case, ImageFinalizedE ventHandler). 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******** *************@d 71g2000cwa.goog legroups.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.Configur ation;
using System.Collecti ons;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Text.Reg ularExpressions ;
using System.IO;
using Toolkit;
public partial class Controls_Select Image : System.Web.UI.U serControl
{
public delegate void ImageFinalizedE ventHandler(obj ect sender,
FileEventArgs e);
public event ImageFinalizedE ventHandler ImageFinalized;

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

//////RaiseBubbleEven t
try
{
////ImageFinalizedE ventHandler eventhandler =
ImageFinalized;
////if (eventhandler != null)
// if (this.ImageFina lized != null)
//ImageFinalized += new ImageFinalizedE ventHandler(
OnImageFinalize d( new FileEventArgs(t his.FileName));
}
catch (Exception ex)
{
throw new Exception(ex.Me ssage);
}

//RaiseEvent ImageFinalized( Me, New
FileHandlingEve ntArgs(FileName ));

}

protected void OnImageFinalize d(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(s tring theFileName):ba se()
{
_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
2245
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 data: a= b= c= d=
28
20281
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()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object...
11
9217
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 C++. I find my self sometimes, trying Object app = Object(); Object *app = Object(); Object app = new Object();
29
2277
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
2503
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: System.Runtime.InteropServices.Marshal.ReleaseComObject(Obj);
16
2878
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 will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList...
7
3292
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class...
3
2756
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? http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Defining_Functions doesn't actually give any insight.
2
2628
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
12074
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
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3449
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.