Detecting what the target object is when NullReferenceException is thrown.

Posted by StingyJack on Stack Overflow See other posts from Stack Overflow or by StingyJack
Published on 2008-09-22T15:35:37Z Indexed on 2010/04/03 23:23 UTC
Read the original article Hit count: 171

Filed under:
|

I'm sure we all have received the wonderfully vague "Object reference not set to instance of an Object" exception at some time or another. Identifying the object that is the problem is often a tedious task of setting breakpoints and inspecting all members in each statement.

Does anyone have any tricks to easily and efficiently identify the object that causes the exception, either via programmatical means or otherwise?

--edit

It seems I was vague like the exception =). The point is to _not have to debug the app to find the errant object. The compiler/runtime does know that the object has been allocated, and that the object has not yet been instantiated. Is there a way to extract / identify those details in a caught exception

@ W. Craig Trader

Your explanation that it is a result of a design problem is probably the best answer I could get. I am fairly compulsive with defensive coding and have managed to get rid of most of these errors after fixing my habits over time. The remaining ones just tweak me to no end, and lead me to posting this question to the community.

Thanks for everyone's suggestions.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about exception