This question already has an answer here:
这个问题已经有了答案:
- What is a NullReferenceException, and how do I fix it? 33 answers
- 什么是NullReferenceException,我如何修复它?33个答案
I am receiving this error and I'm not sure what it means?
我收到这个错误,我不知道它是什么意思?
Object reference not set to an instance of an object.
对象引用没有设置为对象的实例。
8 个解决方案
#1
118
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:
. net中的变量要么是引用类型,要么是值类型。值类型是基本类型,如整数和布尔值或结构(可以识别,因为它们从System.ValueType继承)。布尔变量在声明时具有默认值:
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.
如果您试图使用空引用访问类实例的成员,那么您将获得System.NullReferenceException。与未设置为对象实例的对象引用相同。
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:
如果您正在使用一个API或调用可能返回null的方法,那么有必要优雅地处理这个问题。上面的主要方法可以以这样一种方式进行修改,即用户不应该看到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();
}
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.
以上这些都只是. net类型基础的提示,对于更多信息,我建议您通过c#获取CLR,或者阅读同一作者Jeffrey Richter的这篇MSDN文章。还可以查看更复杂的示例NullReferenceException。
Some teams using Resharper make use of JetBrains attributes to annotate code to highlight where nulls are (not) expected.
一些使用Resharper的团队使用JetBrains属性来注释代码,以突出显示空值(不)的位置。
#2
16
Another easy way to get this:
另一种简单的方法是:
Person myPet = GetPersonFromDatabase();
// check for myPet == null... AND for myPet.PetType == null
if ( myPet.PetType == "cat" ) <--- fall down go boom!
#3
12
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.
不要直言不讳,但它的意思就是它说的。一个对象引用是空的。当您尝试访问NULL d对象的属性或方法时,您将看到这一点。
#4
9
In a nutshell it means.. You are trying to access an object without instantiating it.. You might need to use the "new" keyword to instantiate it first i.e create an instance of it.
简而言之,它的意思是……你试图访问一个对象而不实例化它。您可能需要使用“new”关键字来首先实例化它i。e创建一个实例。
For eg:
如:
public class MyClass
{
public int Id {get; set;}
}
MyClass myClass;
myClass.Id = 0; <----------- An error will be thrown here.. because myClass is null here...
You will have to use:
你必须使用:
myClass = new MyClass();
myClass.Id = 0;
Hope I made it clear..
希望我说得明白。
#5
4
It means you did something like this.
意思是你做了这样的事。
Class myObject = GetObjectFromFunction();
And without doing
而不做
if(myObject!=null)
, you go ahead do myObject.Method();
如果(myObject!=null),则执行myObject. method ();
#6
1
Most of the time, when you try to assing value into object, and if the value is null, then this kind of exception occur. Please check this link.
大多数情况下,当您试图将值保存到对象中时,如果值为null,则会出现这种异常。请检查这个链接。
for the sake of self learning, you can put some check condition. like
为了自学,你可以设置一些检查条件。就像
if (myObj== null)
Console.Write("myObj is NULL");
#7
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.
确切地说,您试图使用一个空对象,就好像它是一个正确引用的对象。
#8
0
If I have the class:
如果我有课:
public class MyClass
{
public void MyMethod()
{
}
}
and I then do:
然后我做:
MyClass myClass = null;
myClass.MyMethod();
The second line throws this exception becuase I'm calling a method on a reference type object that is null
(I.e. has not been instantiated by calling myClass = new MyClass()
)
第二行抛出这个异常,因为我正在调用引用类型对象上的一个方法,该对象是空的(例如,没有通过调用myClass = new myClass()来实例化)
#1
118
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:
. net中的变量要么是引用类型,要么是值类型。值类型是基本类型,如整数和布尔值或结构(可以识别,因为它们从System.ValueType继承)。布尔变量在声明时具有默认值:
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.
如果您试图使用空引用访问类实例的成员,那么您将获得System.NullReferenceException。与未设置为对象实例的对象引用相同。
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:
如果您正在使用一个API或调用可能返回null的方法,那么有必要优雅地处理这个问题。上面的主要方法可以以这样一种方式进行修改,即用户不应该看到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();
}
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.
以上这些都只是. net类型基础的提示,对于更多信息,我建议您通过c#获取CLR,或者阅读同一作者Jeffrey Richter的这篇MSDN文章。还可以查看更复杂的示例NullReferenceException。
Some teams using Resharper make use of JetBrains attributes to annotate code to highlight where nulls are (not) expected.
一些使用Resharper的团队使用JetBrains属性来注释代码,以突出显示空值(不)的位置。
#2
16
Another easy way to get this:
另一种简单的方法是:
Person myPet = GetPersonFromDatabase();
// check for myPet == null... AND for myPet.PetType == null
if ( myPet.PetType == "cat" ) <--- fall down go boom!
#3
12
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.
不要直言不讳,但它的意思就是它说的。一个对象引用是空的。当您尝试访问NULL d对象的属性或方法时,您将看到这一点。
#4
9
In a nutshell it means.. You are trying to access an object without instantiating it.. You might need to use the "new" keyword to instantiate it first i.e create an instance of it.
简而言之,它的意思是……你试图访问一个对象而不实例化它。您可能需要使用“new”关键字来首先实例化它i。e创建一个实例。
For eg:
如:
public class MyClass
{
public int Id {get; set;}
}
MyClass myClass;
myClass.Id = 0; <----------- An error will be thrown here.. because myClass is null here...
You will have to use:
你必须使用:
myClass = new MyClass();
myClass.Id = 0;
Hope I made it clear..
希望我说得明白。
#5
4
It means you did something like this.
意思是你做了这样的事。
Class myObject = GetObjectFromFunction();
And without doing
而不做
if(myObject!=null)
, you go ahead do myObject.Method();
如果(myObject!=null),则执行myObject. method ();
#6
1
Most of the time, when you try to assing value into object, and if the value is null, then this kind of exception occur. Please check this link.
大多数情况下,当您试图将值保存到对象中时,如果值为null,则会出现这种异常。请检查这个链接。
for the sake of self learning, you can put some check condition. like
为了自学,你可以设置一些检查条件。就像
if (myObj== null)
Console.Write("myObj is NULL");
#7
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.
确切地说,您试图使用一个空对象,就好像它是一个正确引用的对象。
#8
0
If I have the class:
如果我有课:
public class MyClass
{
public void MyMethod()
{
}
}
and I then do:
然后我做:
MyClass myClass = null;
myClass.MyMethod();
The second line throws this exception becuase I'm calling a method on a reference type object that is null
(I.e. has not been instantiated by calling myClass = new MyClass()
)
第二行抛出这个异常,因为我正在调用引用类型对象上的一个方法,该对象是空的(例如,没有通过调用myClass = new myClass()来实例化)