Object reference not set to an instance of an object

Tuesday 11 October 2005

I got this error message from .NET. It basically means “null pointer”:

Exception: Object reference not set to an instance of an object.

The more I look at it, the more I hate it. Why are there so many words? Who are we trying to impress? What’s an “instance of an object”? Don’t they mean:

Exception: Object reference not set to an instance of a class.

And isn’t “instance of a class” a fancy way to say “object”? So shouldn’t it be:

Exception: Object reference not set to an object.

And what else would it be set to? Can’t we just shorten this to:

Exception: Object reference not set.

Or why don’t we just say what we mean:

Exception: Null object reference.

Or, (heaven forbid):

Exception: Null pointer.

I know, I know, they aren’t pointers, they’re object references. Whatever. Bite me.


That error message sounds like it was written by an engineer!

Written by an engineer? I don't know. If it *was* written by a fellow engineer, it'd be pretty close to what Ned said:

"Exception: Null pointer. Bite me!"
That's funny. I keep think the same thing. I could live with Null object reference as it uses C# terminology and is to the point.
I agree with Chris and Ned.... "Null object reference" is perfect. The first time I saw this error, I was like "wha...!?" Then I finally deciphered their crazy wording and came to the same conclusion as everyone else.

You might as well change C++'s null pointer error to "Object pointer not set to address of object" :P
It's not perfect, how about telling us what object wasn't set? All we know is that somewhere, there is a null reference. You might as well say "something's wrong"
Brian: this is just the message associated with NullReferenceException. As with NullPointerException in Java the code that catches the exception has access to the stack trace.

It's not clear where Ned saw this message but normally there would be a stack trace too. Code that just prints the exception text and doesn't log the stack is dopey as the message text.
You are too funny! This one got a "What are you laughing at?" from my wife, as I sit here in front of the TV...

In VB6, the same error was "Object variable or With block variable not set". That's not so bad, if you're happy with using "variable" in that way.

The "or With block variable" part is correct, but confusing. Presumably some engineer couldn't resist including that.
Is this error specific to marshalling over the managed boundary?

I think it means the thing returned from an unmanaged call couldn't be coerced into the correct managed type. The most obvious reason would be a null pointer in unmanaged space, but maybe there are other possibilities. A misaligned pointer also springs to mind.

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
Comment text is Markdown.