对象引用未设置为对象的实例 - 解释?

时间:2021-03-21 16:55:03

I am looking for steps/guidance to troubleshoot the error Object reference not set to an instance of an object. and an explanation of why the issue occurs.

我正在寻找步骤/指导来解决错误对象引用未设置为对象的实例。并解释问题发生的原因。

I am looking for a more general explanation, so if I get the error, what steps I should take to find the problem. I often see posts where someone provides a specific piece of code, and someone else will provide the fixed code (sometimes). If simple code examples are provided to illustrate this problem, that's fine.

我正在寻找更一般的解释,所以如果我得到错误,我应该采取什么步骤来找到问题。我经常看到有人提供特定代码的帖子,而其他人则会提供固定代码(有时)。如果提供简单的代码示例来说明这个问题,那很好。

I need a high level explanation.

我需要一个高级别的解释。

5 个解决方案

#1


3  

The easiest way to explain it is that if your object reference (your variable) is null, then you can't access any properties or methods on it without triggering that exception. Here's an example of code that would throw a "Null Ref" exception (as they are called):

解释它的最简单方法是,如果您的对象引用(您的变量)为null,则无法在不触发该异常的情况下访问其上的任何属性或方法。这是一个会抛出“Null Ref”异常的代码示例(因为它们被调用):

string s = null;
int leng = s.Length;

So I define a string as null, but then try to access its Length property. The exception occurs. If I had used a method on that null string like ToString(), the exception would happen as well.

所以我将字符串定义为null,但然后尝试访问其Length属性。发生异常。如果我在像ToString()这样的空字符串上使用了一个方法,那么异常也会发生。

The problem with tracing down this error is that, as you can see from the error text, you can't tell right away where the Null Ref has occurred. You'll have to put in a breakpoint before the exception occurs and walk the code until you find the offending piece.

跟踪此错误的问题在于,正如您从错误文本中看到的那样,您无法立即判断出Null Ref的位置。您必须在异常发生之前插入断点并遍历代码,直到找到有问题的部分。

#2


1  

There are no general steps - such problems typically lie in the logic of your code and vary too much to have "one size fits all" solution.

没有一般步骤 - 这些问题通常存在于代码的逻辑中,并且变化太大而无法实现“一刀切”的解决方案。

One of the things you could do though, is break into the debugger when this exception is thrown (turn-on NullReferenceException under DEBUG/Exceptions), look at which object is null, and try to figure out why it is so, by examining the objects, perhaps go up the call stack and see what the callers look like etc...

你可以做的事情之一就是在抛出这个异常时进入调试器(在DEBUG / Exceptions下打开NullReferenceException),查看哪个对象为null,并试着弄清楚它为什么是这样,通过检查对象,也许上去调用堆栈,看看调用者的样子等等......

#3


0  

It means you have used an object that is currently null or not instantiated yet.

这意味着您已使用当前为null或尚未实例化的对象。

Thats the high level.... basically whatever is breaking likely contains a null value. Put a break point in a step through it and where its breaking is likely what is null.

这是高水平....基本上任何破碎可能包含一个空值。在它的一个步骤中设置一个断点,并且它的中断很可能是null。

#4


0  

If you have an Object, and the Object reference is null, then calling a method or trying to access a property on that object will throw this error. For example..

如果您有一个Object,并且Object引用为null,则调用方法或尝试访问该对象上的属性将引发此错误。例如..

String myString = null;
string anotherString = myString.Substring(0,2); //this will throw an error.

Do you have some code as an example?

你有一些代码作为例子吗?

#5


0  

Being an extremely common error, you will quickly learn how to deal with it. Basically what it means is that you are trying to use something that does not exist.

作为一个非常常见的错误,您将很快学会如何处理它。基本上它意味着你试图使用不存在的东西。

Here is an example:

这是一个例子:

    Dim lstNumbers As List(Of Integer)
    lstNumbers.Add(1)
    lstNumbers.Add(2)
    lstNumbers.Add(3)

When you go to run this code, it will give a null reference exception and provide you with the "Object reference not set to an instance of an object." error. What this means is that you created an object called 'lstNumbers' and told the compiler that it will be a list on integers, but you have not actually created the list. In essence, you created a placeholder, but no object to hold anything.

当您运行此代码时,它将提供一个空引用异常,并为您提供“未将对象引用设置为对象的实例”。错误。这意味着你创建了一个名为'lstNumbers'的对象,并告诉编译器它将是一个整数列表,但你实际上并没有创建列表。实质上,您创建了一个占位符,但没有任何对象可以保存任何内容。

To fix it, you must create the object itself. So in the case of the example above, you would change it to:

要修复它,您必须自己创建对象。因此,对于上面的示例,您可以将其更改为:

    Dim lstNumbers As New List(Of Integer)
    lstNumbers.Add(1)
    lstNumbers.Add(2)
    lstNumbers.Add(3)

The New keyword tells it to create a new list object and assign it to that space. Now you can add to it without any errors. Some objects will initialize themselves such as strings, integers, and others.

New关键字告诉它创建一个新的列表对象并将其分配给该空间。现在您可以添加它而不会出现任何错误。有些对象会初始化自己,如字符串,整数等。

So the best advice for troubleshooting these types of errors is to determine why the object is not set to an instance of itself. Check to make sure you have used either the New function or set it to an existing object, such as:

因此,对这些类型的错误进行故障排除的最佳建议是确定对象未设置为自身实例的原因。检查以确保您已使用New函数或将其设置为现有对象,例如:

    Dim lstNumbers As New List(Of Integer)
    lstNumbers.Add(1)
    lstNumbers.Add(2)
    lstNumbers.Add(3)

    Dim lstNumbers2 As List(Of Integer)
    lstNumbers2 = lstNumbers

You don't need to use New on lstNumbers2 since you are assigning lstNumbers to it. However, if you tried the following, it wouldn't work because lstNumbers has not been initialized:

您不需要在lstNumbers2上使用New,因为您要为其分配lstNumbers。但是,如果您尝试以下操作,则无法正常工作,因为尚未初始化lstNumbers:

    Dim lstNumbers As List(Of Integer)

    Dim lstNumbers2 As List(Of Integer)
    lstNumbers2 = lstNumbers

#1


3  

The easiest way to explain it is that if your object reference (your variable) is null, then you can't access any properties or methods on it without triggering that exception. Here's an example of code that would throw a "Null Ref" exception (as they are called):

解释它的最简单方法是,如果您的对象引用(您的变量)为null,则无法在不触发该异常的情况下访问其上的任何属性或方法。这是一个会抛出“Null Ref”异常的代码示例(因为它们被调用):

string s = null;
int leng = s.Length;

So I define a string as null, but then try to access its Length property. The exception occurs. If I had used a method on that null string like ToString(), the exception would happen as well.

所以我将字符串定义为null,但然后尝试访问其Length属性。发生异常。如果我在像ToString()这样的空字符串上使用了一个方法,那么异常也会发生。

The problem with tracing down this error is that, as you can see from the error text, you can't tell right away where the Null Ref has occurred. You'll have to put in a breakpoint before the exception occurs and walk the code until you find the offending piece.

跟踪此错误的问题在于,正如您从错误文本中看到的那样,您无法立即判断出Null Ref的位置。您必须在异常发生之前插入断点并遍历代码,直到找到有问题的部分。

#2


1  

There are no general steps - such problems typically lie in the logic of your code and vary too much to have "one size fits all" solution.

没有一般步骤 - 这些问题通常存在于代码的逻辑中,并且变化太大而无法实现“一刀切”的解决方案。

One of the things you could do though, is break into the debugger when this exception is thrown (turn-on NullReferenceException under DEBUG/Exceptions), look at which object is null, and try to figure out why it is so, by examining the objects, perhaps go up the call stack and see what the callers look like etc...

你可以做的事情之一就是在抛出这个异常时进入调试器(在DEBUG / Exceptions下打开NullReferenceException),查看哪个对象为null,并试着弄清楚它为什么是这样,通过检查对象,也许上去调用堆栈,看看调用者的样子等等......

#3


0  

It means you have used an object that is currently null or not instantiated yet.

这意味着您已使用当前为null或尚未实例化的对象。

Thats the high level.... basically whatever is breaking likely contains a null value. Put a break point in a step through it and where its breaking is likely what is null.

这是高水平....基本上任何破碎可能包含一个空值。在它的一个步骤中设置一个断点,并且它的中断很可能是null。

#4


0  

If you have an Object, and the Object reference is null, then calling a method or trying to access a property on that object will throw this error. For example..

如果您有一个Object,并且Object引用为null,则调用方法或尝试访问该对象上的属性将引发此错误。例如..

String myString = null;
string anotherString = myString.Substring(0,2); //this will throw an error.

Do you have some code as an example?

你有一些代码作为例子吗?

#5


0  

Being an extremely common error, you will quickly learn how to deal with it. Basically what it means is that you are trying to use something that does not exist.

作为一个非常常见的错误,您将很快学会如何处理它。基本上它意味着你试图使用不存在的东西。

Here is an example:

这是一个例子:

    Dim lstNumbers As List(Of Integer)
    lstNumbers.Add(1)
    lstNumbers.Add(2)
    lstNumbers.Add(3)

When you go to run this code, it will give a null reference exception and provide you with the "Object reference not set to an instance of an object." error. What this means is that you created an object called 'lstNumbers' and told the compiler that it will be a list on integers, but you have not actually created the list. In essence, you created a placeholder, but no object to hold anything.

当您运行此代码时,它将提供一个空引用异常,并为您提供“未将对象引用设置为对象的实例”。错误。这意味着你创建了一个名为'lstNumbers'的对象,并告诉编译器它将是一个整数列表,但你实际上并没有创建列表。实质上,您创建了一个占位符,但没有任何对象可以保存任何内容。

To fix it, you must create the object itself. So in the case of the example above, you would change it to:

要修复它,您必须自己创建对象。因此,对于上面的示例,您可以将其更改为:

    Dim lstNumbers As New List(Of Integer)
    lstNumbers.Add(1)
    lstNumbers.Add(2)
    lstNumbers.Add(3)

The New keyword tells it to create a new list object and assign it to that space. Now you can add to it without any errors. Some objects will initialize themselves such as strings, integers, and others.

New关键字告诉它创建一个新的列表对象并将其分配给该空间。现在您可以添加它而不会出现任何错误。有些对象会初始化自己,如字符串,整数等。

So the best advice for troubleshooting these types of errors is to determine why the object is not set to an instance of itself. Check to make sure you have used either the New function or set it to an existing object, such as:

因此,对这些类型的错误进行故障排除的最佳建议是确定对象未设置为自身实例的原因。检查以确保您已使用New函数或将其设置为现有对象,例如:

    Dim lstNumbers As New List(Of Integer)
    lstNumbers.Add(1)
    lstNumbers.Add(2)
    lstNumbers.Add(3)

    Dim lstNumbers2 As List(Of Integer)
    lstNumbers2 = lstNumbers

You don't need to use New on lstNumbers2 since you are assigning lstNumbers to it. However, if you tried the following, it wouldn't work because lstNumbers has not been initialized:

您不需要在lstNumbers2上使用New,因为您要为其分配lstNumbers。但是,如果您尝试以下操作,则无法正常工作,因为尚未初始化lstNumbers:

    Dim lstNumbers As List(Of Integer)

    Dim lstNumbers2 As List(Of Integer)
    lstNumbers2 = lstNumbers