Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does calling an Action, that is null, throw NullReferenceException?

Why do I have to check if an Action is not null to avoid getting a NullReferenceException? Isn't it logical if there is no action, then it's okay to just do nothing and proceed? I don't understand why it has to throw an exception. Action is a class, why doesn't it handle this itself?

like image 909
user1306322 Avatar asked Sep 11 '13 12:09

user1306322


People also ask

Why am I getting a NullReferenceException?

This infamous and dreaded error message happens when you get a NullReferenceException. This exception is thrown when you try to access a member—for instance, a method or a property—on a variable that currently holds a null reference.

How do I fix NullReferenceException in C#?

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 do I fix NullReferenceException in unity?

This error is caused when an object is trying to be used by a script but does not refer to an instance of an object. To fix this example we can acquire a reference to an instance of the script using GameObject. Find to find the object it is attached to.

How do I fix this error system NullReferenceException object reference not set to an instance of an object?

The best way to avoid the "NullReferenceException: Object reference not set to an instance of an object” error is to check the values of all variables while coding. You can also use a simple if-else statement to check for null values, such as if (numbers!= null) to avoid this exception.


1 Answers

You're trying to invoke an instance method (Invoke) on an object. Doing that always fails with a NullReferenceException if you're using a null reference1. For example:

 object x = null;
 string y = x.ToString();

Should object.ToString() handle that too?

Basically, this is consistent with the way the rest of the type system works. The C# language could have been designed in a different way - potentially only for the "shorthand" of action() meaning action.Invoke() - but it wasn't, and it's not going to change now.

It's easy enough to add your own extension method to Action if you want to:

public static class ActionExtensions
{
    public static void NullSafeInvoke(this Action action)
    {
        if (action != null)
        {
            action();
        }
    }
}

Or use the C# 6 null-conditional operator to only invoke the delegate when the reference is non-null:

myAction?.Invoke();

(This works for any delegate type, not just actions - for EventHandler for example, you'd use something like handler?.Invoke(this, new EventArgs()). The EventArgs() constructor won't even be called if handler is null.)


1 At least using C#. There are ways of invoking instance methods non-virtually "on" null references in IL, but it's far from the normal.

like image 182
Jon Skeet Avatar answered Sep 28 '22 09:09

Jon Skeet