Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why 'Object reference not set to an instance of an object' is not more descriptive?

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

like image 570
joerage Avatar asked Jan 14 '10 16:01

joerage


1 Answers

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.

like image 166
Jon Skeet Avatar answered Sep 22 '22 15:09

Jon Skeet