Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object reference not set to an instance of an object - how to find the offending object name in the exception?

Tags:

c#

.net

This is the bane of my programming existence. After deploying an application, when this error crops up, no amount of debug dump tells you WHAT object was not instantiated. I have the call stack, that's great, it tells me roughly where the object is, but is there any way to get .NET to tell me the actual name of the object?

If you catch them while debugging, of course the program breaks right on the offending creature, but if it happens after the program is in the wild, good luck.

There has to be a way.

I've explored the exceptions returned in these instances and there is just nothing helpful.

like image 890
Jason Avatar asked Apr 16 '10 21:04

Jason


People also ask

How do you solve this object reference not set to an instance of an object?

To fix "Object reference not set to an instance of an object," you should try running Microsoft Visual Studio as an administrator. You can also try resetting the user data associated with your account or updating Microsoft Visual Studio to the latest version.

What is object reference not set to an instance of an object C?

The error means that your code is trying to access/reference an object that is a null valued object that is not there in memory.


1 Answers

No, it's not possible. The exception happens because a reference is null, and references doesn't have names. Variables and class/struct members have names, but it's not certain that the reference is stored in either of those. The reference could for example be created like this:

someObject.GetInstance().DoSomething(); 

If the GetInstance method returns null, there is a null reference exception when you try to use the reference to call DoSomething. The reference is just a return value from the method, it's not stored in a variable, so there is nothing to get a name from.

If you have debugging information in the compiled assembly, you will get the line number in the stack trace in the exception, but that is as close as you can get.

like image 191
Guffa Avatar answered Sep 16 '22 14:09

Guffa