什么"未将对象引用设置到对象&QUOT的实例;意味着? [英] What does "Object reference not set to an instance of an object" mean?

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

问题描述

我收到这个错误,我不知道这是什么意思?

  

对象引用未设置到对象的实例。

解决方案

在.NET中的变量或者是引用类型还是值类型。值类型是原语,如整数和的 或结构(可以识别,因为他们从的System.ValueType )。布尔变量,声明的时候,有一个默认值:

 布尔mybool;
// mybool ==假
 

引用类型,申报的时候,没有一个默认值:

 类ExampleClass中
{
}

例如ExampleClass ExampleClass中; // == NULL
 

如果您尝试使用一个空引用访问类实例中的一员,那么你得到一个<一个href="http://msdn.microsoft.com/en-us/library/system.nullreferenceexception%28loband%29.aspx">System.NullReferenceException.这是一样的对象引用未设置到对象的实例。

下面code是复制这样一个简单的方法:

 静态无效的主要(字串[] args)
{
    VAR ExampleClass中=新ExampleClass中();
    变种returnedClass = exampleClass.ExampleMethod();
    returnedClass.AnotherExampleMethod(); //的NullReferenceException这里。
}

类ExampleClass中
{
    公共ReturnedClass ExampleMethod()
    {
        返回null;
    }
}

类ReturnedClass
{
    公共无效AnotherExampleMethod()
    {
    }
}
 

这是一个非常普遍的错误,并且可能发生,因为各种理由的。根本原因实际上取决于你所遇到的具体方案。

如果您使用的是API或调用可能返回null方法,然后将它处理这个​​重要的正常的。的主要方法上面可以以这样一种方式,的NullReferenceException绝不应被用户看到被修改:

 静态无效的主要(字串[] args)
{
    VAR ExampleClass中=新ExampleClass中();
    变种returnedClass = exampleClass.ExampleMethod();

    如果(returnedClass == NULL)
    {
        //抛出一个有意义的异常或给用户一些有用的反馈!
        返回;
    }

    returnedClass.AnotherExampleMethod();
}
 

以上所有的真的只是暗示.NET类型基础,为进一步的信息,我建议要么拿起的通过C#或阅读这个CLR 通过同一作者MSDN文章 - 杰弗里里希特。另外,请查阅,要复杂得多,例如,当你可能会遇到一个NullReferenceException的。

JetBrains公司属性注释code突出的地方(不)预期的空值。

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

Object reference not set to an instance of an object.

解决方案

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

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.

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

All of the above really just hints at .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 this, much more complex, example of when you can encounter a NullReferenceException.

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

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

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