For the record, here is what one of the program managers for the CLR had to say about it
The NullReferenceException occurs because an instruction like “call [eax+44]” or “mov edx, [esi+24]” has resulted in an access violation. We don’t retain nearly enough information to form a correspondence between a particular register being NULL at a particular EIP and the fact that a particular reference in the application was null. Especially since the EIP might be in a shared helper like a write barrier routine or an array helper. In those cases, we would have to perform a limited stack walk to get the effective EIP
The machinery that would be required to improve this error message is huge. For the foreseeable future, you will have to rely on debuggers, or on FX code explicitly checking and throwing an appropriate NullArgumentException
Chris.
http://blogs.msdn.com/cbrumme
----- Bryan Livingston wrote: ----
I'm guessing that NullReferenceExceptions are by far the most common exceptions thrown in .net
Why not add the type of the reference to the message? Have it say something like
Object reference of type 'string' not set to an instance of an object
This would really help when trying to figure out where the null reference is happening
I've just spent the last two days finding a null reference that happens very intermittently. Having the type would have saved me many hours
In addition to just the type, you might be able to use reflection to give a property name or variable name
Bryan Livingsto
Alphora.com
Thanks for your suggestion. I will add this to our list for future
considerations.
Once suggestion is create a trace/log system where you can log activities
on the web site to help debug the problem.
Thanks,
Kelvin
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>Thread-Topic: NullReferenceException Message Feature Request
>thread-index: AcP4rnQ2fwitLh6SR/CMAirGLsHYkQ==
>X-Tomcat-NG: microsoft.public.dotnet.framework.clr
>From: "=?Utf-8?B?QnJ5YW4gTGl2aW5nc3Rvbg==?="
<anonymous@discussions.microsoft.com>
>References: <6FDA50BD-7BF4-43C6-AD92-6DE18207002C@microsoft.com>
>Subject: RE: NullReferenceException Message Feature Request
[quoted text clipped - 17 lines]
>
>For the record, here is what one of the program managers for the CLR had to say about it:
The NullReferenceException occurs because an instruction like ???call
[eax+44]??? or ???mov edx, [esi+24]??? has resulted in an access violation.
We don???t retain nearly enough information to form a correspondence
between a particular register being NULL at a particular EIP and the fact
that a particular reference in the application was null. Especially since
the EIP might be in a shared helper like a write barrier routine or an
array helper. In those cases, we would have to perform a limited stack
walk to get the effective EIP.
The machinery that would be required to improve this error message is huge.
For the foreseeable future, you will have to rely on debuggers, or on FX
code explicitly checking and throwing an appropriate NullArgumentException.
Chris.
http://blogs.msdn.com/cbrumme
----- Bryan Livingston wrote: -----
I'm guessing that NullReferenceExceptions are by far the most common
exceptions thrown in .net.
Why not add the type of the reference to the message? Have it say
something like:
Object reference of type 'string' not set to an instance of an object.
This would really help when trying to figure out where the null
reference is happening.
I've just spent the last two days finding a null reference that
happens very intermittently. Having the type would have saved me many
hours.
In addition to just the type, you might be able to use reflection to
give a property name or variable name.
Bryan Livingston
Alphora.com