Take the 2-minute tour ×

I know that when a NullReferenceException gets thrown, the error message states:

Object reference not set to an instance of an object.

And I realize this message is thrown when I attempt to dereference a null object reference.

The error message implies that there could be a reason for a NullReferenceException besides an object reference being null (perhaps a bad memory address or something similar). Is this the case?

Edit: I'm more concerned with the reasons aNullReferenceException could be thrown than the wording of the error message. The wording of the error message is just what prompted the question.

share|improve this question
Consider rephrasing this question to focus on possible causes of NREs "besides an object reference being null", because the reason for the precise wording of the error message cannot objectively be answered (except by the .NET design team, perhaps :P), and this question will likely be closed as too subjective. –  Dan J Jan 17 '11 at 19:40
Subjective and argumentative? Really? –  Robert Harvey Jan 17 '11 at 19:42
@djacobson: Updated the title of the question. –  Andrew Whitaker Jan 17 '11 at 19:43
add comment

closed as not constructive by Ed S., Darin Dimitrov, Neil Knight, Otávio Décio, Graviton Jan 18 '11 at 12:10


4 Answers

up vote 4 down vote accepted

I suspect it's trying to be language-neutral. A Visual Basic programmer is used to "nothing" instead of "null", for example. Obviously the type name isn't language neutral, but at least if the message is, that's a start.

I don't know that you'd get this if somehow you tried to dereference a "bad" memory address... at that point there's a far worse CLR error involved.

Another possibility is that it's trying to avoid stating that you've explicitly set the value of the reference to null - it could be just the default value of a reference type variable, for example.

share|improve this answer
"I don't know that you'd get this if somehow you tried to dereference a "bad" memory address"... This is fun, actually. Dereferencing a pointer with address < 65536 will throw a NullReferenceException, and above that will either work or throw an AccessViolationException. Addresses < 65536 are reserved in Windows, along with the top 65536 addresses. msdn.microsoft.com/en-us/library/ms810627.aspx –  Porges Jan 17 '11 at 19:58
@Porges: So this exception could occur when dereferencing an invalid pointer address? How would you even make that happen? –  Andrew Whitaker Jan 17 '11 at 21:12
int g = *((int*)12345); - will need to be in an unsafe context –  Porges Jan 17 '11 at 21:53
@Porges, @Jon: Thanks for the answer, this was helpful and answered my question. –  Andrew Whitaker Jan 18 '11 at 17:57
add comment

Saying the reference is set to null doesn't say a whole lot about what needs to be done. In their attempt to be more "helpful", they are telling you what needs to happen: The object reference needs to be set to an instance of an object.

I don't really read that as implying there could be other reasons besides a reference being null. If it was left uninitialized, it causes a compile error. The bottom line is that the reference doesn't refer to anything at the time you attempted to use it.

share|improve this answer
add comment

When an object is declared like so:

MyClass MyObject;

it is defined as an object reference. However, because it is null (and has not been instantiated), it is not set to an instance of an object. If we add:

MyObject = new MyClass();

then the reference has been set to a new instance of the object.

If we later set it to null:

MyObject = null;

Then it is again in the null state. Therefore, the exception message covers both cases (not instantiated or explicitly set to null).

share|improve this answer
add comment

It's part of the Common Language Specification. Take a look at the difference between a static method and an instance method.

A static method belongs to the Type, and as such does not require an instance. If you look at the IL byte code, static methods are referred to as "call".

However, a non-static method is by definition an instance method and requires an instance. The IL instruction is "callvirt". The key difference between "call" and "callvirt" is that "callvirt" checks to see that the target to invoke the method against is not null.

share|improve this answer
You don't have to use callvirt to call an instance method. You can use call, and indeed "this" can be null within an instance method, if it's called in that way. The C# compiler (almost) always calls instance methods with callvirt, but I believe it's entirely legal IL to use call instead. –  Jon Skeet Jan 17 '11 at 20:14
I suppose you can generate IL via emit, but I don't think marking something as sealed or internal is going to prevent the compiler from not generating callvirt statements, no? If not, cite. –  bryanbcook Jan 17 '11 at 22:31
add comment
