473,320 Members | 1,821 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,320 software developers and data experts.

NullReferenceException handler throws NullReferenceException!

I just got the strangest error. I have exception code that catches a
null reference exception:
catch (NullReferenceException ex) { ... }
The handling code does nothing but print out data from within the
object, ex. So, nothing is changed. And I've found that the
exception object, ex, ITSELF is null!

So, by accessing it to print its innards, it throws another
NullReferenceException (which is, of course, unhandled)! Printing
ex.Message actually works, it reports "Object reference not set to an
instance of an object." But, accessing ex.InnerException is what
throws the null reference, since ex is null.

Strange. I'll see if I can replicate this, and nail it down.

Zytan

Mar 19 '07 #1
7 1956
Strange. I'll see if I can replicate this, and nail it down.

I think the null reference was from ex.InnerException being null. And
I was trying to print ex.InnerException.Message. However, what threw
me off was that viewing ex in the debugger shows:

ex {"Object reference not set to an instance of an object."}
System.NullReferenceException

This made me believe ex was null. (Of course, it would show "null" if
it was null.) But, if the object referenece in not set to an instance
of an object, then WTH is it set to? In the debugger, under ex, it
shows no methods, just "base". Under that, "base" again. Finally,
under that, it shows some methods.

Zytan

Mar 19 '07 #2
But, if the object referenece in not set to an instance
of an object, then WTH is it set to?
LOL! It's telling me that the exception IS that an object was
referenced that was null (not set to an instance of an object).

Zytan

Mar 19 '07 #3

"Zytan" <zy**********@yahoo.comwrote in message
news:11*********************@y66g2000hsf.googlegro ups.com...
>Strange. I'll see if I can replicate this, and nail it down.

I think the null reference was from ex.InnerException being null. And
I was trying to print ex.InnerException.Message. However, what threw
me off was that viewing ex in the debugger shows:

ex {"Object reference not set to an instance of an object."}
System.NullReferenceException

This made me believe ex was null. (Of course, it would show "null" if
it was null.) But, if the object referenece in not set to an instance
of an object, then WTH is it set to? In the debugger, under ex, it
shows no methods, just "base". Under that, "base" again. Finally,
under that, it shows some methods.
The debugger shows ex.ToString(), which shows you ex.Message. ex is indeed
an instance of NullReferenceException, with InnerException=null and
Message="Object reference not set to an instance of an object."
>
Zytan

Mar 19 '07 #4
"Zytan" <zy**********@yahoo.comwrote in message
news:11*********************@d57g2000hsg.googlegro ups.com...
>I just got the strangest error. I have exception code that catches a
null reference exception:
catch (NullReferenceException ex) { ... }
The handling code does nothing but print out data from within the
object, ex. So, nothing is changed. And I've found that the
exception object, ex, ITSELF is null!

So, by accessing it to print its innards, it throws another
NullReferenceException (which is, of course, unhandled)! Printing
ex.Message actually works, it reports "Object reference not set to an
instance of an object." But, accessing ex.InnerException is what
throws the null reference, since ex is null.

Strange. I'll see if I can replicate this, and nail it down.
ex isn't null, InnerException is.

Mar 19 '07 #5
The debugger shows ex.ToString(), which shows you ex.Message. ex is indeed
an instance of NullReferenceException, with InnerException=null and
Message="Object reference not set to an instance of an object."
Yup, thanks Ben, I figured it out somewhat myself. But, yeah, it's
nice to know that ToString() returns Message.

Zytan

Mar 19 '07 #6
So, by accessing it to print its innards, it throws another
NullReferenceException (which is, of course, unhandled)! Printing
ex.Message actually works, it reports "Object reference not set to an
instance of an object." But, accessing ex.InnerException is what
throws the null reference, since ex is null.

ex isn't null, InnerException is.
Indeed.

Zytan

Mar 19 '07 #7

"Zytan" <zy**********@yahoo.comwrote in message
news:11*********************@n76g2000hsh.googlegro ups.com...
>The debugger shows ex.ToString(), which shows you ex.Message. ex is
indeed
an instance of NullReferenceException, with InnerException=null and
Message="Object reference not set to an instance of an object."

Yup, thanks Ben, I figured it out somewhat myself. But, yeah, it's
nice to know that ToString() returns Message.
Peeking at mscorlib with the insanely useful .NET Reflector
(http://www.aisto.com/roeder/dotnet/), we can see exactly how
Exception.ToString() works:

public override string ToString()
{
string text2;
string message = this.Message;
if (this._className == null)
{
this._className = this.GetClassName();
}
if ((message == null) || (message.Length <= 0))
{
text2 = this._className;
}
else
{
text2 = this._className + ": " + message;
}
if (this._innerException != null)
{
text2 = text2 + " ---" + this._innerException.ToString() +
Environment.NewLine + " " +
Environment.GetResourceString("Exception_EndOfInne rExceptionStack");
}
if (this.StackTrace != null)
{
text2 = text2 + Environment.NewLine + this.StackTrace;
}
return text2;
}
>
Zytan

Mar 19 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: TT (Tom Tempelaere) | last post by:
Hi, Once in a while my application throws an NullReferenceException at startup, however it appears to be occurring in an unknown module. If I debug it and ask to 'break', then there is no source...
0
by: Matthias Kwiedor | last post by:
Hi! I want to create a WebBrowser Control at runtime. So i ported the "at start creation" of the Control to a new routine. Everything works fine, but i implement the BeforeNavigateFix and a...
1
by: Eduard Ralph | last post by:
Hi, I have a weird exception happening when calling the new operator. I'm not sure if this is the intended reaction to the situation or is actually a problem I've got. In any case a different...
0
by: Bob | last post by:
I have several simple user controls (e.g. header, footer) that have static content so I thought it would be a good idea to turn on the ouput cache. So I added <%@ OutputCache Duration="600"...
0
by: theta | last post by:
Hi everyone, I'm a newbie .NET developer with vb6 experience. I was toying myself with the Howl open source library for Zeroconf networks and I stumbled across an exception I can't quite figure...
0
by: theta | last post by:
Hi everyone. I'm a .NET newbie and I have a problem calling DLL functions from vb .net. I'm trying to access Howl's Rendezvous.dll (www.porchdogsoft.com/products/howl/) from managed code. I was...
0
by: Ryan Liu | last post by:
I have a program works fine in .NET 1.1 and just recompiled in .NET 2.0 without any code change. Compiles OK, but there is an exeception when execute it. Then I remove PK, it works all fine...
3
by: Sagar | last post by:
Hi. I am working on a project to migrate a web application from 1.1 to 2.0 Within in the DAL of the application, there is a call to below function that builds a command object for later use. ...
6
by: puzzlecracker | last post by:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an ob ject. Here is where I get this: public void Foo(){ ArrayList list=new ArrayList();...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.