231

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

Object reference not set to an instance of an object.

4

8 回答 8

195

.NET 中的变量要么是引用类型,要么是值类型。值类型是诸如整数布尔值 或结构之类的原语(并且可以识别,因为它们继承自System.ValueType)。布尔变量在声明时具有默认值:

bool mybool;
//mybool == false

引用类型在声明时没有默认值:

class ExampleClass
{
}

ExampleClass exampleClass; //== null

如果您尝试使用空引用访问类实例的成员,则会得到System.NullReferenceException。这与Object reference not set to an instance of an object相同。

以下代码是重现此问题的简单方法:

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()
    {
    }
}

这是一个非常常见的错误,可能由于各种原因而发生。根本原因实际上取决于您遇到的具体情况。

如果您正在使用 API 或调用可能返回 null 的方法,那么优雅地处理这一点很重要。上面的 main 方法可以修改为 NullReferenceException 永远不会被用户看到:

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();
}

以上所有内容实际上只是 .NET 类型基础知识的提示,有关更多信息,我建议通过 C# 学习 CLR或阅读同一作者 - Jeffrey Richter 的这篇MSDN 文章。另请查看更复杂的示例,了解何时会遇到 NullReferenceException。

一些使用 Resharper 的团队使用JetBrains 属性来注释代码,以突出显示(不)预期的空值。

于 2009-04-22T20:51:51.207 回答
28

简而言之,这意味着..您正在尝试访问一个对象而不实例化它..您可能需要先使用“new”关键字来实例化它,即创建它的一个实例。

例如:

public class MyClass
{
   public int Id {get; set;}
}

MyClass myClass;

myClass.Id = 0; <----------- An error will be thrown here.. because myClass is null here...

您将不得不使用:

myClass = new MyClass();
myClass.Id = 0;

希望我说清楚了..

于 2014-11-10T08:24:53.207 回答
23

另一种简单的方法:

 Person myPet = GetPersonFromDatabase();
 // check for myPet == null... AND for myPet.PetType == null
 if ( myPet.PetType == "cat" ) <--- fall down go boom!
于 2009-04-22T21:39:21.060 回答
10

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.

于 2009-04-22T20:34:26.787 回答
5

It means you did something like this.

Class myObject = GetObjectFromFunction();

And without doing

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

于 2009-04-22T20:36:29.213 回答
3

如果我有课:

public class MyClass
{
   public void MyMethod()
   {

   }
}

然后我做:

MyClass myClass = null;
myClass.MyMethod();

第二行抛出此异常,因为我正在调用引用类型对象的方法null(即尚未 通过调用实例化myClass = new MyClass()

于 2013-08-23T10:45:49.250 回答
2

大多数时候,当您尝试将值赋值给对象时,如果该值为 null,则会发生这种异常。请检查此链接

为了自学,你可以放一些检查条件。喜欢

if (myObj== null)
Console.Write("myObj is NULL");
于 2009-04-22T20:49:25.593 回答
1

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.

于 2009-04-22T20:36:33.537 回答