Advertisement
Advertisement


What does "Object reference not set to an instance of an object" mean?


Question

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

Object reference not set to an instance of an object.

2014/04/04
1
193
4/4/2014 5:25:18 PM


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!
2014/10/23

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..


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.

2009/04/22

It means you did something like this.

Class myObject = GetObjectFromFunction();

And without doing

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

2013/08/23

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.

2009/04/22

Source: https://stackoverflow.com/questions/779091
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]