[Solved-5 Solutions] What is a NullReferenceException, and how do we fix it?



Error Description:

    • We have some code and when it executes, it throws a NullReferenceException, saying:
    • Object reference not set to an instance of an object.
    • What does this mean, and what can we do to fix this error?

    Solution 1:

    • The runtime throwing a NullReferenceException always means the same thing: we are trying to use a reference, and the reference is not initialized (or it was once initialized, but is no longer initialized).
    • This means the reference is null, and we cannot access members (such as methods) through a null reference. The simplest case:
    string foo = null;
    foo.ToUpper();
    
    click below button to copy the code. By - C# tutorial - team
    • This will throw a NullReferenceException at the second line because we can't call the instance method ToUpper() on a string reference pointing to null.

    Solution 2:

    • Since the problem is an object reference which is Nothing, the answer is to examine them to find out which one. Then determine why it is not initialized. Hold the mouse over the various variables and Visual Studio (VS) will show their values - the culprit will be Nothing.
    • We should also remove any Try/Catch blocks from the relevant code, especially ones where there is nothing in the Catch block. This will cause our code to crash when it tries to use an object which is Nothing. This is what we want because it will identify the exact location of the problem, and will allow us to identify the object causing it.

    Solution 3:

    • The New operator can often be used to create the instance when you declare it:
    Dim reg As New CashRegister        ' [New] creates instance, invokes the constructor
    
    ' Longer, more explicit form:
    Dim reg As CashRegister = New CashRegister
    
    click below button to copy the code. By - C# tutorial - team
    • When it is only appropriate to create the instance later:
    Private reg As CashRegister         ' Declare
      ...
    reg = New CashRegister()            ' Create instance
    
    click below button to copy the code. By - C# tutorial - team

    Note: Do not use Dim again in a procedure, including the constructor (Sub New):

    Solution 4:

    • Another scenario is when we cast a null object into a value type . For example, the code below:
    object o = null;
        DateTime d = (DateTime)o;
    
    click below button to copy the code. By - C# tutorial - team
    • It will throw a NullReferenceException on the cast. It seems quite obvious in the above sample, but this can happen in more "late-binding" intricate scenarios where the null object has been returned from some code we don't own, and the cast is for example generated by some automatic system.

    Solution 5:

    • JetBrains' Resharper tool will identify every place in your code that has the possibility of a null reference error, allowing you to put in a null check.

    Related Searches to What is a NullReferenceException, and how do we fix it?