271

I am receiving this error and I'm not sure what it means?

Object reference not set to an instance of an object.

2
  • 1
    Almost all cases of NullReferenceException are the same. Please see "What is a NullReferenceException in .NET?" for some hints. Commented Apr 4, 2014 at 17:25
  • I got same error when creating new view, after restarting the Visual Studio, it is gone. Commented Mar 7, 2022 at 17:08

8 Answers 8

216

Variables in .NET are either reference types or value types. Value types are primitives such as integers and booleans or structures (and can be identified because they inherit from System.ValueType). Boolean variables, when declared, have a default value:

bool mybool;
//mybool == false

Reference types, when declared, do not have a default value:

class ExampleClass
{
}

ExampleClass exampleClass; //== null

If you try to access a member of a class instance using a null reference then you get a System.NullReferenceException. Which is the same as Object reference not set to an instance of an object.

The following code is a simple way of reproducing this:

static void Main(string[] args)
{
    var exampleClass = new ExampleClass();
    var returnedClass = exampleClass.ExampleMethod();
    returnedClass.AnotherExampleMethod(); //NullReferenceException here.
}

class ExampleClass
{
    public ReturnedClass ExampleMethod()
    {
        return null;
    }
}

class ReturnedClass
{
    public void AnotherExampleMethod()
    {
    }
}

This is a very common error and can occur because of all kinds of reasons. The root cause really depends on the specific scenario that you've encountered.

If you are using an API or invoking methods that may return null then it's important to handle this gracefully. The main method above can be modified in such a way that the NullReferenceException should never be seen by a user:

static void Main(string[] args)
{
    var exampleClass = new ExampleClass();
    var returnedClass = exampleClass.ExampleMethod();

    if (returnedClass == null)
    {
        //throw a meaningful exception or give some useful feedback to the user!
        return;
    }

    returnedClass.AnotherExampleMethod();
}

All of the above really just hints of .NET Type Fundamentals, for further information I'd recommend either picking up CLR via C# or reading this MSDN article by the same author - Jeffrey Richter. Also check out, much more complex, example of when you can encounter a NullReferenceException.

Some teams using Resharper make use of JetBrains attributes to annotate code to highlight where nulls are (not) expected.

Sign up to request clarification or add additional context in comments.

3 Comments

What if I have a list inside another class, like this: pastebin.com/aWhfu8UM. I get a Run-time exception (line 9): Object reference not set to an instance of an object. error
Since the tag is .net but the language is not specified, in VB.Net the common error is to simply say Dim exampleClass As exampleClass rather than Dim exampleClass As New exampleClass.
+1 - Thank you that was very in depth and helpful! My problem was I had not initiated the class I had created before calling a sub-procedure but this helped me figure it out!
43

In a nutshell it means.. You are trying to access an object without instantiating it.. You might need to use the "new" keyword to instantiate it first i.e create an instance of it.

For eg:

public class MyClass
{
   public int Id {get; set;}
}

MyClass myClass;

myClass.Id = 0; <----------- An error will be thrown here.. because myClass is null here...

You will have to use:

myClass = new MyClass();
myClass.Id = 0;

Hope I made it clear..

1 Comment

I think this answer is the best answer for NullException. It saved one my projects.
23

Another easy way to get this:

 Person myPet = GetPersonFromDatabase();
 // check for myPet == null... AND for myPet.PetType == null
 if ( myPet.PetType == "cat" ) <--- fall down go boom!

1 Comment

Jay's answer demonstrates another example that isn't covered by any of the other answers here.
9

Not to be blunt but it means exactly what it says. One of your object references is NULL. You'll see this when you try and access the property or method of a NULL'd object.

2 Comments

He can be doing throw new NullReferenceException(); lol...
It means exactly what it says IF you know what an object is, a reference and instance... all very loaded words when it comes to programming.
7

It means you did something like this.

Class myObject = GetObjectFromFunction();

And without doing

if(myObject!=null), you go ahead do myObject.Method();

1 Comment

Object reference not set to an instance of an object
4

If I have the class:

public class MyClass
{
   public void MyMethod()
   {

   }
}

and I then do:

MyClass myClass = null;
myClass.MyMethod();

The second line throws this exception becuase I'm calling a method on a reference type object that is null (I.e. has not been instantiated by calling myClass = new MyClass())

Comments

2

Most of the time, when you try to assing value into object, and if the value is null, then this kind of exception occur. Please check this link.

for the sake of self learning, you can put some check condition. like

if (myObj== null)
Console.Write("myObj is NULL");

Comments

2

what does this error mean? Object reference not set to an instance of an object.

exactly what it says, you are trying to use a null object as if it was a properly referenced object.

1 Comment

how to correct this error?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.