Is there a way to find out what specific object caused a NullReferenceException
? I've read the page about troubleshooting NullReferenceException
s and it talks about inspecting variables in the debugger and looking at the exception message.
What if the exception was thrown in production code so you can't run a debugger to inspect the variables? The exception message shows the stack trace so you can see what method the exception was thrown in, but it does not say which specific object was null
.
I'd like to be able to add the name of the object that was null
to the error message so that when I'm looking into reports from users and I come across a NullReferenceException
, I can easily see what object was null
and fix it. Does anyone know of a way to do this?
I also found this question which asked the same thing, but it was from 2011 and I don't know if anything has changed since then.
Edit: The question that this is flagged as a duplicate is indeed a duplicate but is also very old (2008). Has anything changed since then?
Edit 2: I found this when googling this question. Visual Studio can tell you what threw the NullReferenceException
; is there any way to tap into this to add it to a log file?
Find out which exception you're receiving, and either catch that specific exception, or else prevent it from occurring in the first place. You should never catch NullReferenceException.
Solutions to fix the NullReferenceException To prevent the NullReferenceException exception, check whether the reference type parameters are null or not before accessing them. In the above example, if(cities == null) checks whether the cities object is null or not.
Use Null Coalescing to Avoid NullReferenceExceptions It works with all nullable data types. The following code throws an exception without the null coalescing. Adding “?? new List<string>()” prevents the “Object reference not set to an instance of an object” exception.
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null . A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios: You've forgotten to instantiate a reference type.
It should be relatively easy to figure out given the stacktrace, but a better approach would be to include "validation" or parameters and/or null checks in your code and explicitly throw a ArgumentNullException
yourself before you try to access a member of a variable that may not has been initialized. You can then supply the name of the uninitialized object:
if (obj == null)
throw new ArgumentNullException(nameof(obj));
It's a common practise to perform these checks on arguments in both constructors and methods, e.g:
public void SomeMethod(SomeType someArgument)
{
if (someArgument == null)
throw new ArgumentNullException(nameof(someArgument));
//you will never get there if someArgument is null...
var someThing = someArgument.SomeMember;
if (someThing == null)
throw new ArgumentException("SomeMember cannot be null.", nameof(someArgument));
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With