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 to fix this error?
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 to fix this error?
It means your code used an object reference variable that was set to null (i.e. it did not reference an actual object instance).
To prevent the error, objects that could be null should be tested for null before being used.
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 simply ignore these ones):
An example of this exception being thrown is: When you are trying to check something, that is null.
For example:
The .NET runtime will throw a NullReferenceException when you attempt to perform an action on something which hasn't been instantiated i.e. the code above.
In comparison to an ArgumentNullException which is typically thrown as a defensive measure if a method expects that what is being passed to it is not null.
More information is in C# NullReferenceException and Null Parameter.
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 to 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:
object == null
.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.
You are using the object that contains the null value reference. So it's giving a null exception. In the example the string value is null and when checking its length, the exception occurred.
Example:
The exception error is:
NullReferenceException or Object reference not set to an instance of an object occurs when an object of the class you are trying to use is not instantiated. For example:
Assume that you have a class named Student.
Now, consider another class where you are trying to retrieve the student's full name.
As seen in the above code, the statement Student s - only declares the variable of type Student, note that the Student class is not instantiated at this point. Hence, when the statement s.GetFullName() gets executed, it will throw the NullReferenceException.