Why can't the debugger/runtime tell me which object is null?

c#, debugging, nullreferenceexception, visual-studio, visual-studio-2013

Solution

Because the debugger or the compiler have the source symbols, so they can map a name to an address.

The runtime, however, doesn't know how a reference was named in your source code (it has been compiled).

Note that you if you (not the CLR) throwed the NullReferenceException, then you could have added any information in the embedded message.

Problem

Code: ``` items.FirstOrDefault(x => x.Foo.Bar.BarId == snuh.BarId); ``` Error: System.NullReferenceException: Object reference not set to an instance of an object. The null object could be `items`, `Foo`, `Bar`, or `snuh`. The debugger/runtime can tell me on which line of code the error is occurring. Why can't it also tell me which object is the problem? Note: I know I can debug this and find out the answer. Is there a reason Visual Studio can't provide me with the name of the offending object?

Original source

Related problems