Is there any way to find the object name that caused the control to flow into the catch block from a NullReferenceException, so that we can easily debug by giving an alert or logging the object that was null?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
No.
You only get the stack trace including line numbers.
This helps you in simple cases like this:
var result = myString.Trim();
But it doesn’t help in lines like this:
var result = myObj.Method1().Method2();
Method 2
(NOTE: The answer is for the original question, before it was edited four years after the fact)
What is an object’s name? It’s a design-time token to us, programmers, which identifies the object reference, but it has meaning only until compilation.
Some objects have a dedicated Name property but that does not (and should not) have anything to do with the name of the object reference in code, it’s the visual designer’s courtesy to name object references after the Name property, but it’s a convention rather than a requirement. Besides, a null reference cannot have Name property simply because the reference is null, it’s not yet assigned to any object that has a readable Name.
Method 3
Well you can look at the stack trace, reflect the function, and display the parameters and have a guess based on stack state, but I’d say not really as the object could be anywhere in the function.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0