Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you capture the name of the object that throws a NullReferenceException?

Is there a way to find out what specific object caused a NullReferenceException? I've read the page about troubleshooting NullReferenceExceptions 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?

like image 448
Lews Therin Avatar asked Mar 29 '19 14:03

Lews Therin


People also ask

Can NullReferenceException be caught?

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.

How do you handle 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.

How can we avoid system NullReferenceException object reference not set to an instance of an object?

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.

What does system NullReferenceException mean?

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.


1 Answers

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));
    ...
}
like image 66
mm8 Avatar answered Oct 09 '22 00:10

mm8