I have a problem with a uge solution at work that gets a lot of “Object reference not set to an instance of an object” errors. What’s the best way to determine the null object(s) causing the exception?
I can try catch all those exceptions in one place, but can’t find a way to determine the member that is null so I can fix it properly.
try {
}
catch (Exception ex)
{
if (ex is ReferenceNullException)
ex.??
}
}
Since I can view the stacktrace it would be reasonable to think you could also get what caused the error.
Think about it for a second. It’s a NullReferenceException. That means you’re trying to call a method or access a property on a NULL REFERENCE to an object. That means the object reference you’re trying to access is EMPTY, null. It does not exist.
So what you’re trying to find actually does not exist.
Normally to track down which object reference is null a debugger is used. Just set a breakpoint on the line causing the exception and inspect all variables to see which one is null.
Debugger is your greatest tool.