Is there a way to find out which object raised a NullReferenceException?

Is there a way to find the name of the object that causes the control to flow into the catch block from the NullReferenceException so that we can easily debug it by issuing a warning or by registering an object that was null?

+6
source share
3 answers

Not.
You get only the stack trace, including line numbers.
This will help you in simple cases:

var result = myString.Trim(); 

But this does not help on such lines:

 var result = myObj.Method1().Method2(); 
+8
source

What is an object name? This is a development-time token for us programmers that identifies a reference to an object, but only makes sense before compilation.

Some objects have a dedicated Name property, but do not (and should not) have anything to do with the name of the object link in the code, this visual designer kindly named object links after the Name property, but this is more a convention than a requirement. In addition, a null reference cannot have the Name property simply because the reference is null; it has not yet been assigned to any object that has a readable name.

+1
source

Well, you can look at the stack trace, flip the function and display the parameters and guess based on the state of the stack, but I would say that this is not so, since the object can be anywhere in the function.

0
source

Source: https://habr.com/ru/post/894528/


All Articles