What is the meaning of NullReferenceException
Possible Duplicate:
What is a NullReferenceException in .NET?
For example, " System.NullReferenceException
was unhandled", with Message "Object reference not set to an instance of an object."
What is the meaning of this exception, and how can it be solved?
It means you've tried to access a member of something that isn't there:
string s = null;
int i = s.Length; // boom
Just fix the thing that is null. Either make it non-null, or perform a null-test first.
There is also a corner-case here related to Nullable<T>
, generics and the new
generic constraint - a bit unlikely though (but heck, I hit this issue!).
This is the most common exception in .NET... it just mean that you try to call a member of a variable that is not initialized (null). You need to initialize this variable before you can call its members
It means you're referencing something that's null
, for example:
class Test
{
public object SomeProp
{
get;
set;
}
}
new Test().SomeProp.ToString()
SomeProp
will be null and should throw a NullReferenceException
. This is commonly due to code you are calling expecting something to be there that isn't.