What was the design decision behind NullReferenceException not containing any runtime specific information except base class data (like a stacktrace)? And is there an extension for Visual Studio that can tell you straight away which part of an expression was null?
-
2If I remember well, ReSharper can tell you if an expression can throw such an exception.Etienne de Martel– Etienne de Martel2011-01-17 23:36:22 +00:00Commented Jan 17, 2011 at 23:36
-
4Reshaper can predict that something may throw such an exception.jcolebrand– jcolebrand2011-01-17 23:41:43 +00:00Commented Jan 17, 2011 at 23:41
-
You are both right. But ReSharper, even if is a very good helper, has both false positives and false negatives. Often I had to manually disable the notice by comment, because I was mathematically sure about the impossibility of such an exception.usr-local-ΕΨΗΕΛΩΝ– usr-local-ΕΨΗΕΛΩΝ2011-01-17 23:52:29 +00:00Commented Jan 17, 2011 at 23:52
-
@djechelon ~ Hence the reason I counter-qualified his statement. It can't know that it will happen, it can only know that it may happen (your comment)jcolebrand– jcolebrand2011-01-17 23:53:36 +00:00Commented Jan 17, 2011 at 23:53
-
@drachenstern: don't worry, I upvoted your comment, and I just wanted to expand it with mine, highlighting the fact that it's possible to also have fake negatives (you can predict it may, but can you predict that a statement won't throw an exception?). Simply that ;)usr-local-ΕΨΗΕΛΩΝ– usr-local-ΕΨΗΕΛΩΝ2011-01-17 23:59:35 +00:00Commented Jan 17, 2011 at 23:59
6 Answers
An NRE is a very low-level exception. It is a hardware exception (a 'trap') generated by the processor when it is asked to read data from an address below 64K. That region of the virtual memory space is always unmapped, specifically to trap pointer bugs. It starts as an AccessViolation and gets turned into NRE by the CLR when the address is less than 0x00010000. At that point there is very little context for the exception, all that's known is the address of the machine code instruction that caused the trap.
Reverse-engineering that machine code instruction address back to a named variable in your program isn't possible. It is important that it works that way, a jitter would have to generate very inefficient code otherwise. All that can be reasonably done is recover the source code line number. That requires debugging info (a .pdb) that contains line number info. The CLR knows how to read the .pdb file and uses it to generate the exception's stack trace. However, this is often still inaccurate due to optimizations performed by the JIT optimizer, it moves code around. You'll only get a guaranteed match for the Debug build. The reason why the PDB for the Release build doesn't contain source line number info. You can change that.
This problem has a very simple solution. Check for null yourself and generate your own exception before letting the runtime do it. The test is quite cheap, well less than a nanosecond.
8 Comments
null when an NRE occurs. Any idea how this might be possible after all? blogs.msdn.microsoft.com/dotnet/2016/08/02/…You can setup Visual Studio to break on Throw for NullReferenceException immediately and not first at the catch block.
- Debug
- Exceptions
- Common Language
- Runtime Exceptions System
- System.NullReferenceException (Check the box)
Then you will have a break in the line which caused the NullReferenceException.
3 Comments
There's no trivial way to determine what was null at runtime -- the necessary information would have to be decompiled and inferred from the IL, which is sufficiently low-level that all it knows is that "the item at the top of the stack is null" but not how that item got there.
(And I don't know of such an extension, but it would be a nice debugger enhancement)
2 Comments
NullReferenceException is thrown by the runtime, not by the language. The runtime doesn't know where the reference came; it just knows that an instruction tried to use a null reference.
In fact, the exception is "probably" the result of a native windows "invalid access violation" SEH exception, which is caught by the runtime and translated into IL exception ("probably": I have not checked if this is the case, but it's my guess of what's the most performing way to JIT the code).
Comments
Not all information about your code is available at runtime. For example, local variable names aren't available. So, it is impossible to throw exception with message like 'local variable myObj is null, but it's member has been called'. Moreover there are lot of non-user-written objects at runtime (for example classes, generated for closures/anonymous types/iterators etc.) which also could be a source of null-ref exceptions.
10 Comments
If you have debug symbols, you can track down the line that caused the exception. If it's simple enough, you'll be straight getting your null value. Otherwise (think about a.Value = b.Do(c.GetX(),d.GetY(z.ToString()));) you must debug with your IDE