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.

链接地址: http://www.djcxy.com/p/28038.html

上一篇: 尝试使用Request.QueryString时获取对象引用错误

下一篇: NullReferenceException的含义是什么?