“未将对象引用设置为对象的实例"是什么意思?意思? [英] What does "Object reference not set to an instance of an object" mean?

查看:28
本文介绍了“未将对象引用设置为对象的实例"是什么意思?意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误,但不确定它的含义?

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

未将对象引用设置为对象的实例.

Object reference not set to an instance of an object.

推荐答案

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

Variables in .NET are either reference types or value types. Value types are primitives such as integers and booleans or structures (and can be identified because they inherit from System.ValueType). Boolean variables, when declared, have a default value:

bool mybool;
//mybool == false

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

Reference types, when declared, do not have a default value:

class ExampleClass
{
}

ExampleClass exampleClass; //== null

如果您尝试使用空引用访问类实例的成员,则会得到 System.NullReferenceException.这与未将对象引用设置为对象的实例相同.

If you try to access a member of a class instance using a null reference then you get a System.NullReferenceException. Which is the same as Object reference not set to an instance of an object.

以下代码是重现此内容的简单方法:

The following code is a simple way of reproducing this:

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

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

This is a very common error and can occur because of all kinds of reasons. The root cause really depends on the specific scenario that you've encountered.

如果您使用的 API 或调用可能返回 null 的方法,那么优雅地处理这种情况很重要.上面的主要方法可以修改为用户永远不会看到 NullReferenceException:

If you are using an API or invoking methods that may return null then it's important to handle this gracefully. The main method above can be modified in such a way that the NullReferenceException should never be seen by a user:

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 类型基础的提示,有关更多信息,我建议您选择 CLR 通过 C# 或阅读此MSDN 文章 由同一作者 - Jeffrey Richter 撰写.另请查看更复杂的示例可能会遇到 NullReferenceException.

All of the above really just hints of .NET Type Fundamentals, for further information I'd recommend either picking up CLR via C# or reading this MSDN article by the same author - Jeffrey Richter. Also check out, much more complex, example of when you can encounter a NullReferenceException.

一些使用 Resharper 的团队利用 JetBrains 属性 来注释代码以突出显示期望(不)空值的地方.

Some teams using Resharper make use of JetBrains attributes to annotate code to highlight where nulls are (not) expected.

这篇关于“未将对象引用设置为对象的实例"是什么意思?意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆