views:

139

answers:

3

As a developer, we often encounter that exception: NullReferenceException with the well known error message:

Object reference not set to an instance of an object

Is it not possible for the .NET framework to return something a little bit more meaningful?

Something such as:

Object of type X named Y not set to an instance of an object

+1  A: 

Because its the most generic way of saying that.

Tony
+15  A: 

An object doesn't have a name - so how can it tell you the name? The null reference may have been loaded from a variable - or it might have been returned by a method, property etc.

The JIT could probably look at the stack information to work out what the declared reference type was, but I'm not sure how often this would help - and doubtless it would slow things down.

I can't say I've ever found this to be a huge burden when it comes to debugging - if there are lots of things which can be null on a single line, that usually suggests that it's worth splitting them up more.

Jon Skeet
Thanks to the extremely detailed stack trace I don't think it needs to be more detailed. Plus they really should be using asserts to avoid this in the first place.
ChaosPandion
Any kind of debugging slows things down. I wonder, though, whether it would slow things down *that much* compared to how much it would speed things up not to have to go back and break out all the objects into separate lines, or go crazy with the immediate window to figure out which object is null?
Kyralessa
Agreed that this problem isn't all that burdensome. The one or two lines before the one where the null reference exception took place will tell you exactly what's not being initialized.
Paul Sasik
It's not a "huge burden"; but it's a small burden that occurs frequently.
Kyralessa
How frequently are we talking? And how much extra burden does it really place on you? I've very rarely felt this has had any sort of significant effect on my coding speed.
Jon Skeet
Well, personally, I don't believe in instantiating objects until I actually get a NullReferenceException, so this slows me down all the time trying to figure out which reference is giving the exception. I say, just declare the references, leave them null, and hope for the best.
Kyralessa
@Kyralessa: Well, that sounds like it's your development methodology which is flawed, not Visual Studio or .NET. You should know what you're doing - if you need an object, make sure you've got one. You shouldn't just "hope for the best" - development isn't pinball.
Jon Skeet
I'm joking, Jon.
Kyralessa
@Kyralessa: I'm glad. If you'd seen some of the strange suggestions I've read in the past, you'd understand why I didn't assume it was humour :)
Jon Skeet
+2  A: 

To me this seems like purely an issue of preference. If it bugs you that much, you could always subclass the NullReferenceException and change the message. :)

What it comes down to though is the amount of information available when the message is created. Without manually passing in some extra information, simply determining the name of the field that was null inside the NullReferenceException would require reflection (possibly quite a bit), which is a rather heavy-load operation. Take for example the ArgumentNullException, in order to throw one in which the message indicates the offending parameter name, you have to pass in a string when you call it: throw new ArgumentNullException("param1").

The level of detail in an exception message is entirely up to the programmer who defines it, but it's prudent to avoid doing anything system intensive when throwing an exception.

Nathan Taylor
I am pretty sure the framework throws the NullReferenceException.
ChaosPandion
It does, but you could always `try { } catch(NullReferenceException e) { throw new CoolerNullReferenceException(e) }` or some such thing.
Nathan Taylor
Ah I just noticed the smiley in your answer. :)
ChaosPandion