Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object reference not set to an instance of an object. 3

Status
Not open for further replies.

Tokhra

Programmer
Oct 1, 2003
134
ES
Hi all,

Im getting "Object reference not set to an instance of an object" when im calling the keyword "this".

I can't understand how this is even possible, when if im calling "this" from within a class, it must be set to an instance of an object..

Is there scope on where you can call "this" from? e.g. im referencing it from a try catch block within a private method of the class.

Any ideas?

Thanks,
Matt.
 
yes, for example a static member function,

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
This isn't in a static member function,

private void SomeFunction()
{
try
{
// do something
}
catch(Exception e)
{
// Raise error event.
OnError(this, "Error: " + e.ToString());
}
}

thats basically what I have, in a public class. Error is a event:

public delegate ErrorEventHandler(object sender, string error);

public event ErrorEventHandler OnError;

Any ideas?
 
It is "OnError" itself that is null, not the "this" reference.

OnError is null because no class has registered for the event at the time you fire the event. If you have some class register for the event, you will see the null problem vanish (temporarily).

The best way to deal with this is to use a firing routine, like "FireOnError" to fire the event. This routine would look like this:

// FYI: A class derived from EventArgs would be a more
// conventional argument, instead of a string, for
// your delegate and handler, but I stayed
// with the string as you had it, to be consistent
// with the example.
private void FireOnError(string error)
{
if (OnError != null) {
OnError(this,error);
}
}

With that method in place, your try/catch code looks like this:

private void SomeFunction()
{
try
{
// do something
}
catch(Exception e)
{
// Raise error event.
FireOnError("Error: " + e.ToString());
}
}

--

If you only fire the event in the catch handler, you could also just do it like this (of course):


private void SomeFunction()
{
try
{
// do something
}
catch(Exception e)
{
// Raise error event.
if (OnError != null)
{
OnError(this, "Error: " + e.ToString());
}
}
}

but the first approach is the way it is more commonly done.

By the way, the static method theory was a good guess, but if a reference to "this" had been attempted from within a static function, the compiler would have bounced it.

In C++ it is actually possible to have a null "this" pointer, but I don't think it is possible in pure C# without unsafe blocks etc. In general, if you think you have a null "this" in C#, look somewhere else.
 
Great, thanks Cheiron. I did actually figure this out in the end but forgot to mention it on here.

Your method of event firers seems like a logical idea, I think I'll stick with that in anything I develop from now on.

Thanks for the reply ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top