I 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 I do about it?
I have some code and when it executes, it throws a What does this mean, and what can I do about it? |
||||
show 8 more comments |
It means your code used an object reference that was set to null (i.e. it did not refer to an actual object instance). To prevent the error, objects that could be null should be tested for null before being used. |
||||
|
It means that the variable in question is pointed at nothing. I could generate this like so:
That will throw the error because while I've declared the variable "connection", it's not pointed at anything. When I try to call the member "Open", there's no reference for it to resolve, and it will throw the error. To avoid this error:
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. This error is the number one source of bugs, IMHO. |
|||||||||||
|
ExamplesGeneric
If ref1 or ref2 or ref3 is null, then you'll get a NullReferenceException. If you want to solve the problem, then find out which one is null:
You can step through your code with the debugger to see which is null, or just let it run, and the exception message will tell you which of those lines is the culprit. Specifically, in Simple
Indirect
Array
Array Elements
Collection/List/Dictionary
Range Variable (Indirect/Deferred)
Bad Naming Conventions:
If you named fields differently from locals, you might have realized that you never initialized the field. Suggestion:
ASP.NET Page Life cycle:
ASP.NET Session Values
Ways to AvoidExplicitly check for
|
|
@John Saunders, not to be super critical but this is such a basic question that if someone is searching for it they need a really, really basic answer. I'd recommend changing your classes from abstract A's and B's into concrete things like Person and Employee or something similar. Probably have a VB version, too. – Chris Haas Jan 11 '11 at 17:15 |
||
|
@Chris: constructive criticism is welcome. I also want an array example. – John Saunders Jan 11 '11 at 17:17 |
||
|
@Chris: worth noting that he made this answer CW... If you have something to contribute, you should consider editing it. – Shog9♦ Jan 11 '11 at 19:36 |
||
|
I added the "array elements" example, inspired by stackoverflow.com/q/4660103 – Justin Jan 13 '11 at 21:37 |
||
|
@Random832 I'd also add that although a try block is free, exceptions are incredibly expensive and involve unwinding the stack - so they should be kept to an absolute minimum and never as part of the intended application flow – Basic Dec 21 '12 at 18:22 |
Note, regardless of the scenario, the cause is always the same in .NET: You are trying to use a reference variable who’s value is Nothing/null. When the value is Nothing/null for the reference variable, that means it is not actually holding a reference to an instance of any object that exists on the heap. You either never assigned something to the variable, never created an instance of the value assigned to the variable, or you set the variable equal to Nothing/null manually, or you called a function that set the variable to Nothing/null for you. |
|||
|
An example of this exception being thrown is; when you are trying to check something, that is null. for example:
Other peoples code (e.g. code inside the .net framework) may throw a NullReferenceException, where the code cannot do what it needs to if your code sets it to null i.e. it expects something other than null. More info here: http://dotnetperls.com/nullreferenceexception |
|||||||||||||||||
|
Another case that I don't see mentioned here in the answers is when you cast a null object into a value type. For example, the code below:
Will throw a One example of this is this simple ASP.NET binding fragment with the Calendar control:
Here, |
||||
|
you are using the object that contain the null value reference. so its giving null exception. in the example the string value is null and when check its length the exception occured. Example:
The exception error is: Unhandled Exception:
|
||||
|
Another case where
Here, In general, you should use a cast or If you are expecting the type conversion to always succeed (ie. you know what the object should be ahead of time), then you should use a cast:
If you are unsure of the type, but you want to try to use it as a specific type, then use
|
|||
|
If you have not initialized a Reference Type, and you want to set or read one of its properties, it will throw a NullReferenceException. Example:
You can simply avoid this by checking if the variable is not null:
To fully understand why a NullReferenceException is thrown, it is important to know the difference between Value Types and Reference Types. So, if you're dealing with Value Types, NullReferenceExceptions can not occur. Though you need to keep alert when dealing with Reference Types! Only Reference Types, as the name is suggesting, can hold references or point literally to nothing (or 'null'). Whereas Value Types always contain a value. Reference Types: (These ones must be checked)
Value Types: (You can easily ignore these ones)
|
|||||||||||
|
NullReferenceException
since the earliest pre-betas of .NET 1.0. – John Saunders Jan 11 '11 at 17:00