Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't object reference error exceptions in .net tell me which object was null?

Maybe asking the question betrays my lack of knowledge about the process, but then again, there's no better reason to ask!

Tracking these down can be frustrating because stack traces can help me know where to start looking but not which object was null.

What is going on under the hood here? Is it because the variable names aren't bundled in the executable?

like image 517
jrsconfitto Avatar asked Jan 07 '11 14:01

jrsconfitto


People also ask

How do you catch a null reference exception?

Being a plain old C# exception, NullReferenceException can be caught using a try/catch : try { string s = null; s. ToString(); } catch (NullReferenceException e) { // Do something with e, please. }

Can an object reference be null?

One of the main causes of bugs with null reference is the fact that in C every reference type object can be null, all the time.

Which method is used to check object reference is null?

requireNonNull() Method to Check if Object Is Null in Java The requireNonNull() method is also a static method of the Objects class. This method accepts an object reference and throws a NullPointerException if the reference is null.


2 Answers

.NET code built with full optimizations and no debug info: your local variable names are gone, some local variables may have been eliminated entirely.

.NET code built with full optimizations + PDB (or full debug): most local variable names preserved, some local variables may have been eliminated

No optimizations + no debug info: local variable names are gone.

And then we have to consider that whatever you're dealing with may not be in a local variable at all - it might have been the result of a previous function call, on which you're chaining a new function call.

like image 78
Damien_The_Unbeliever Avatar answered Oct 15 '22 22:10

Damien_The_Unbeliever


Basically you answered your own question. When you're code is compiled it's transformed in intermediate language (IL). IL does not have variable names the way your code does, arguments to a method being called are pushed on to a stack before the method is called and the currents methods arguments and local variables are referred to by there position. I believe this is because this structure aids the JIT compiler generate code.

The pdb symbols file stores a mapping between the IL generated and your code. It is used to tell you which line in your code each method call in the call stack refers to. Possibly the information stored here isn't detailed enough to say which variable is null, or possibly it was just considered too expensive in terms when of perf to be able to do this. In any case, if you have allowed the compiler to optimize the IL generated there may no longer be a one to one mapping between the variables in the IL and the variables in your code.

Hope that helps, Rob

like image 27
Robert Avatar answered Oct 15 '22 22:10

Robert