tags:

views:

942

answers:

8

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

Object reference not set to an instance of an object.

+5  A: 

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.

dkpatt
He can be doingthrow new NullReferenceException();lol...
Jhonny D. Cano -Leftware-
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.
Gavin Miller
Jhonny - hahaha I guess you're right
dkpatt
+1  A: 

It means you did something like this.

Class myObject = GetObjectFromFunction();

And without doing

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

J.W.
+1  A: 

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.

TStamper
+1  A: 

dkpatt is right, but also, that is the message associated with a NullReferenceException

for a more detailed explanation, see @Iain's response as to common sources of the NullReferenceException

Darren Kopp
I would suggest that this http://stackoverflow.com/questions/779091/what-does-object-reference-not-set-to-an-instance-of-an-object-mean/779184#779184 is a better answer.
Robert Kozak
soo... you downvote me for giving additional information to the prior accepted answer (in which case mine became the accepted answer) because another person posted another answer after mine? well done sir. (and his just has code snippets, but fundamentally comes out the exact same, it's a NullReferenceException)
Darren Kopp
I am new to StackOverflow but I thought the idea was to have better answers flow to the top. You may have been first although his answer is similar it is a better more complete answer. Maybe I'll just not worry about downvoting and hope the rest of the crowd upvotes.
Robert Kozak
Downvoting should only really be used in extreme circumstances. Like when the answer is incorrect.
Iain
Note that you currently get -2 rep for giving someone else a downvote.
Iain
Yeah I noticed that. oh well. :-S
Robert Kozak
downvoting is for answers that are wrong really. answered questions are ALWAYS on top, then ranked by what you have as sort condition (votes is default). i fully concur that Iain's response is better, but i don't have control over what is accepted or not. the idea is best answers will rise to the top (as is shown here also), but you can cast multiple votes for a reason.
Darren Kopp
+1  A: 

Here is a thread explaining the common causes of the "Object reference not set to an instance of an object" error.

Michael Kniskern
+1  A: 

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");
Syed Tayyab Ali
+15  A: 

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 at .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 this, much more complex, example of when you can encounter a NullReferenceException.

Iain
+1 Great Answer! I down voted some of the others to help this one to the top. This is the kind of answer I would like to see when I am looking at questions on StackOverFlow.
Robert Kozak
Heh you downvoted other good answers to make this one bubble up?
Robert S.
@Robert - that sounds like a very negative thing to do. Downvote if they are unhelpful, misleading or wrong, please don't downvote just because you prefer another answer. Other people took time to give answers and some of them look perfectly accurate and do not deserve or require a downvote.
Steve Haigh
@Robert, thanks for the support! Downvoting is a bit extreme though! Bit unfair for Darren to get negative rep especially since he did post before me.
Iain
I'll not worry so much about downvoting. I didn't think downvotes affected so much the rep. I'm new here. I assumed thats how it was supposed to work.
Robert Kozak
+1  A: 

Another easy way to get this:

 Person x = GetPersonFromDatabase();
 // check for x == null... AND for Person.Pet == null
 if ( Person.Pet == "cat" ) <--- fall down go boom!
Jay
Jay's answer demonstrates another example that isn't covered by any of the other answers here.
Iain