什么是 NullReferenceException,如何修复它?
我有一些代码,当它执行时,它抛出一个 NullReferenceException ,说:
未将对象引用设置为对象的实例。
这是什么意思?我该如何修复此错误?
I have some code and when it executes, it throws a NullReferenceException
, saying:
Object reference not set to an instance of an object.
What does this mean, and what can I do to fix this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(26)
原因是什么?
底线
您正在尝试使用
null
(或 VB.NET 中的Nothing
)的内容。这意味着您要么将其设置为null
,要么根本不将其设置为任何内容。与其他任何东西一样,
null
也会被传递。如果方法“A”中的值为null
,则可能是方法“B”将null
传递给了方法“A”。null
可以有不同的含义:NullReferenceException
。null
来表示没有可用的有意义的值。请注意,C# 具有变量可为空数据类型的概念(例如数据库表可以具有可为空字段) -您可以将null
分配给它们以指示其中没有存储任何值,例如int? a = null;
(这是Nullablea = null;
的快捷方式),其中问号表示允许在变量中存储null
一个
。您可以使用if (a.HasValue) {...}
或if (a==null) {...}
进行检查。可空变量(如本示例中的a
)允许通过a.Value
显式访问值,或者像平常一样通过a
访问值。注意,如果
,通过
是a.Value
访问它会抛出InvalidOperationException
而不是NullReferenceException
anull
- 您应该事先进行检查,即如果您有另一个不可为 null 的变量int b;
那么您应该进行像这样的赋值if (a.HasValue) { b = a.Value; }
或更短的if (a != null) { b = a; }
。本文的其余部分将更详细地介绍许多程序员经常犯的错误,这些错误可能会导致
NullReferenceException
。更具体地说,
runtime
抛出NullReferenceException
always 意味着同样的事情:您正在尝试使用引用,并且该引用未初始化(或者它曾经初始化过,但不再初始化)。这意味着该引用为
null
,并且您无法通过null
引用访问成员(例如方法)。最简单的情况:这将在第二行抛出
NullReferenceException
,因为您无法在string
引用上调用实例方法ToUpper()
指向null
。调试
如何找到
NullReferenceException
的来源?除了查看异常本身(异常将在异常发生的位置准确抛出)之外,Visual Studio 中的调试一般规则也适用:放置策略断点和 检查变量,方法是将鼠标悬停在变量名称上、打开(快速)监视窗口或使用各种调试面板(如 Locals)和汽车。如果您想找出引用的设置或未设置的位置,请右键单击其名称并选择“查找所有引用”。然后,您可以在每个找到的位置放置一个断点,并使用附加的调试器运行程序。每次调试器在此类断点处中断时,您都需要确定是否希望引用为非空,检查变量,并验证它是否在您希望时指向实例。
通过这种方式遵循程序流程,您可以找到实例不应该为空的位置,以及为什么没有正确设置它。
示例
一些可能引发异常的常见场景:
通用
如果 ref1 或 ref2 或 ref3 为 null,那么您将收到
NullReferenceException
。如果您想解决该问题,请通过将表达式重写为其更简单的等效项来找出哪个为 null:具体来说,在
HttpContext.Current.User.Identity.Name
中,HttpContext. Current
可以为 null,或者User
属性可以为 null,或者Identity
属性可以为 null。间接
如果您想避免子(Person)空引用,您可以在父(Book)对象的构造函数中初始化它。
嵌套对象初始值设定项
同样适用于嵌套对象初始值设定项:
这意味着:
使用
new
关键字时,它仅创建Book
的新实例,而不是新实例Person
,因此Author
属性仍为null
。嵌套集合初始值设定
项 嵌套集合
Initializers
的行为相同:这意味着:
new Person
仅创建Person
的实例,但>Books
集合仍为null
。集合Initializer
语法不会创建集合对于
p1.Books
,它仅转换为p1.Books.Add(...)
语句。数组
数组元素
交错数组
集合/列表/字典
范围 变量(间接/延迟)
事件 (C#)
(注意:VB.NET 编译器插入对事件使用情况的空检查,因此无需检查事件是否无任何内容 code> 在 VB.NET 中。)
错误的命名约定:
如果您对字段的命名与本地字段的命名不同,您可能会意识到您从未初始化过该字段。
这可以通过遵循以下划线前缀字段的约定来解决:
ASP.NET 页面生命周期:
ASP.NET 会话值
ASP.NET MVC 空视图模型
如果引用
@ModelASP.NET MVC View
中,您需要了解当您返回
视图时,Model
会在您的操作方法中设置。当您从控制器返回空模型(或模型属性)时,视图访问它时会发生异常:WPF 控件创建顺序和事件
WPF
控件在调用InitializeComponent< 期间创建/code> 按照它们在可视化树中出现的顺序。如果早期创建的控件具有事件处理程序等,并且在引用后期创建的控件的
InitializeComponent
期间触发,则会引发NullReferenceException
。例如:
这里
comboBox1
是在label1
之前创建的。如果comboBox1_SelectionChanged
尝试引用label1
,则它尚未创建。更改
XAML
中声明的顺序(即,在comboBox1
之前列出label1
,忽略设计理念问题)至少可以解决 <代码>NullReferenceException在这里。使用
as
进行强制转换这不会引发
InvalidCastException
,但在强制转换失败时(以及当someObject
时)返回null
本身为空)。所以要注意这一点。LINQ
FirstOrDefault()
和SingleOrDefault()
普通版本
First()
和Single()
会在出现异常时抛出异常没什么。在这种情况下,“OrDefault”版本返回null
。所以要注意这一点。foreach
当您尝试迭代
null
集合时,foreach
会抛出异常。通常是由返回集合的方法产生意外的null
结果引起的。更实际的示例 - 从 XML 文档中选择节点。如果未找到节点但初始调试显示所有属性均有效,则会抛出异常:
的方法
避免显式检查
null
并忽略null
值。如果您预计引用有时为
null
,则可以在访问实例成员之前检查它是否为null
:显式检查
null
并提供默认值。您调用的期望实例的方法可能会返回
null
,例如,当无法找到正在查找的对象时。在这种情况下,您可以选择返回默认值:从方法调用中显式检查
null
并引发自定义异常。您还可以抛出自定义异常,仅在调用代码中捕获它:
如果值永远不应该为
null
,请使用Debug.Assert
,以便在出现问题之前捕获问题。发生异常。当您在开发过程中知道某个方法可以但永远不应该返回
null
时,您可以使用Debug.Assert()
在它确实发生时尽快中断:此检查不会最终出现在您的发布版本中,导致它抛出在发布模式下运行时,当
book == null
时再次出现NullReferenceException
。对
可为 null
值类型使用GetValueOrDefault()
,以便在它们为null
时提供默认值。使用空合并运算符:
??
[C#] 或If()
[VB]。遇到
null
时提供默认值的简写:对数组使用 null 条件运算符:
?.
或?[x]
(在 C# 6 和 VB.NET 14 中可用):有时也称为安全导航或 Elvis(以其形状命名)运算符。如果运算符左侧的表达式为 null,则不会计算右侧,而是返回 null。这意味着像这样的情况:
如果此人没有头衔,则会抛出异常,因为它试图对具有 null 值的属性调用
ToUpper
。在 C# 5 及以下版本中,可以通过以下方式进行保护:
现在 title 变量将为 null,而不是引发异常。 C# 6 为此引入了更短的语法:
这将导致 title 变量为
null
,并且如果person.Title
则不会调用ToUpper
code> 为null
。当然,您仍然必须检查
title
是否为 null 或将 null 条件运算符与 null 合并运算符一起使用 (??
)来提供默认值:同样,对于数组,您可以使用
?[i]
,如下所示:这将执行以下操作:如果
myIntArray
是null
,表达式返回null
,您可以安全地检查它。如果它包含一个数组,它将执行以下操作:elem = myIntArray[i];
并返回第 ith 元素。使用 null 上下文(在 C# 8 中可用):
在
C# 8
中引入,null 上下文和可为 null 的引用类型对变量执行静态分析,并在值可能为 null时提供编译器警告code> 或已设置为null
。可空引用类型允许显式允许类型为null
。可以使用
csproj
文件中的Nullable
元素为项目设置可为空注释上下文和可为空警告上下文。此元素配置编译器如何解释类型的可为空性以及生成哪些警告。有效设置为:enable
:启用可为空注释上下文。可以为空的警告上下文已启用。引用类型(例如字符串)的变量是不可为 null 的。所有可空性警告均已启用。disable
:可空注释上下文被禁用。可为空的警告上下文被禁用。引用类型的变量是不可见的,就像 C# 的早期版本一样。所有可空性警告均被禁用。safeonly
:启用可空注释上下文。可为空的警告上下文是安全的。引用类型的变量不可为 null。所有安全可空性警告均已启用。警告
:可空注释上下文已禁用。可以为空的警告上下文已启用。引用类型的变量是不可见的。所有可空性警告均已启用。safeonlywarnings
:可空注释上下文已禁用。可为空的警告上下文是安全的。引用类型的变量是不可见的。所有安全可空性警告均已启用。
可空引用类型使用与可为空值类型相同的语法进行标记:将
?
附加到变量的类型。用于调试和修复迭代器中空解引用的特殊技术
C#
支持“迭代器块”(在其他一些流行语言中称为“生成器”)。由于延迟执行,NullReferenceException
在迭代器块中调试可能特别棘手:如果
whatever
结果为null
,则MakeFrob
会抛出。现在,您可能认为正确的做法是:为什么这是错误的?因为迭代器块直到
foreach
才真正运行!对 GetFrobs 的调用只是返回一个对象,该对象在迭代时将运行迭代器块。通过像这样编写
null
检查,您可以防止出现NullReferenceException
,但可以将NullArgumentException
移动到迭代点,没有达到调用的程度,这调试起来非常混乱。正确的修复方法是:
也就是说,创建一个具有迭代器块逻辑的私有帮助器方法和一个执行
null
检查并返回迭代器的公共表面方法。现在,当调用GetFrobs
时,null
检查立即发生,然后在迭代序列时执行GetFrobsForReal
。如果您检查
LINQ
to Objects 的参考源,您将发现该技术自始至终都在使用。它写起来稍微有点笨拙,但它使调试无效错误变得更加容易。 优化代码是为了调用者的方便,而不是为了作者的方便。关于不安全代码中的 null 取消引用的说明
C#
有一种“不安全”模式,顾名思义,这种模式极其危险,因为提供内存安全和类型安全的正常安全机制并未强制执行。 除非您对内存的工作原理有透彻而深入的了解,否则不应编写不安全的代码。在不安全模式下,您应该了解两个重要事实:
要了解其原因,首先需要了解 .NET 如何产生 NullReferenceException。 (这些详细信息适用于在 Windows 上运行的 .NET;其他操作系统使用类似的机制。)
Windows
中的内存是虚拟化的;每个进程都会获得由操作系统跟踪的许多内存“页”组成的虚拟内存空间。内存的每个页面上都设置了标志,用于确定如何使用它:读取、写入、执行等。 最低页面被标记为“如果以任何方式使用都会产生错误”。C# 中的空指针和空引用在内部都表示为数字零,因此任何将其取消引用到其相应内存存储的尝试都会导致操作系统产生错误。然后 .NET 运行时会检测到此错误并将其转换为 NullReferenceException。
这就是为什么取消引用空指针和空引用会产生相同的异常。
那么第二点呢?取消引用位于虚拟内存最低页中的任何无效指针都会导致相同的操作系统错误,从而导致相同的异常。
为什么这是有道理的?好吧,假设我们有一个包含两个 int 的结构,以及一个等于 null 的非托管指针。如果我们尝试取消引用结构中的第二个 int,CLR 将不会尝试访问位置 0 处的存储;它将访问位置四的存储。但从逻辑上讲,这是一个 null 取消引用,因为我们通过 null 访问该地址。
如果您正在使用不安全的代码并且收到
NullReferenceException
,请注意有问题的指针不必为 null。可以是最底层页面的任意位置,都会产生该异常。What is the cause?
Bottom Line
You are trying to use something that is
null
(orNothing
in VB.NET). This means you either set it tonull
, or you never set it to anything at all.Like anything else,
null
gets passed around. If it isnull
in method "A", it could be that method "B" passed anull
to method "A".null
can have different meanings:NullReferenceException
.null
intentionally to indicate there is no meaningful value available. Note that C# has the concept of nullable datatypes for variables (like database tables can have nullable fields) - you can assignnull
to them to indicate there is no value stored in it, for exampleint? a = null;
(which is a shortcut forNullable<int> a = null;
) where the question mark indicates it is allowed to storenull
in variablea
. You can check that either withif (a.HasValue) {...}
or withif (a==null) {...}
. Nullable variables, likea
in this example, allow to access the value viaa.Value
explicitly, or just as normal viaa
.Note that accessing it via
a.Value
throws anInvalidOperationException
instead of aNullReferenceException
ifa
isnull
- you should do the check beforehand, i.e. if you have another non-nullable variableint b;
then you should do assignments likeif (a.HasValue) { b = a.Value; }
or shorterif (a != null) { b = a; }
.The rest of this article goes into more detail and shows mistakes that many programmers often make which can lead to a
NullReferenceException
.More Specifically
The
runtime
throwing aNullReferenceException
always means the same thing: you are trying to use a reference, and the reference is not initialized (or it was once initialized, but is no longer initialized).This means the reference is
null
, and you cannot access members (such as methods) through anull
reference. The simplest case:This will throw a
NullReferenceException
at the second line because you can't call the instance methodToUpper()
on astring
reference pointing tonull
.Debugging
How do you find the source of a
NullReferenceException
? Apart from looking at the exception itself, which will be thrown exactly at the location where it occurs, the general rules of debugging in Visual Studio apply: place strategic breakpoints and inspect your variables, either by hovering the mouse over their names, opening a (Quick)Watch window or using the various debugging panels like Locals and Autos.If you want to find out where the reference is or isn't set, right-click its name and select "Find All References". You can then place a breakpoint at every found location and run your program with the debugger attached. Every time the debugger breaks on such a breakpoint, you need to determine whether you expect the reference to be non-null, inspect the variable, and verify that it points to an instance when you expect it to.
By following the program flow this way, you can find the location where the instance should not be null, and why it isn't properly set.
Examples
Some common scenarios where the exception can be thrown:
Generic
If ref1 or ref2 or ref3 is null, then you'll get a
NullReferenceException
. If you want to solve the problem, then find out which one is null by rewriting the expression to its simpler equivalent:Specifically, in
HttpContext.Current.User.Identity.Name
, theHttpContext.Current
could be null, or theUser
property could be null, or theIdentity
property could be null.Indirect
If you want to avoid the child (Person) null reference, you could initialize it in the parent (Book) object's constructor.
Nested Object Initializers
The same applies to nested object initializers:
This translates to:
While the
new
keyword is used, it only creates a new instance ofBook
, but not a new instance ofPerson
, so theAuthor
property is stillnull
.Nested Collection Initializers
The nested collection
Initializers
behave the same:This translates to:
The
new Person
only creates an instance ofPerson
, but theBooks
collection is stillnull
. The collectionInitializer
syntax does not create a collectionfor
p1.Books
, it only translates to thep1.Books.Add(...)
statements.Array
Array Elements
Jagged Arrays
Collection/List/Dictionary
Range Variable (Indirect/Deferred)
Events (C#)
(Note: The VB.NET compiler inserts null checks for event usage, so it's not necessary to check events for
Nothing
in VB.NET.)Bad Naming Conventions:
If you named fields differently from locals, you might have realized that you never initialized the field.
This can be solved by following the convention to prefix fields with an underscore:
ASP.NET Page Life cycle:
ASP.NET Session Values
ASP.NET MVC empty view models
If the exception occurs when referencing a property of
@Model
in anASP.NET MVC View
, you need to understand that theModel
gets set in your action method, when youreturn
a view. When you return an empty model (or model property) from your controller, the exception occurs when the views access it:WPF Control Creation Order and Events
WPF
controls are created during the call toInitializeComponent
in the order they appear in the visual tree. ANullReferenceException
will be raised in the case of early-created controls with event handlers, etc., that fire duringInitializeComponent
which reference late-created controls.For example:
Here
comboBox1
is created beforelabel1
. IfcomboBox1_SelectionChanged
attempts to referencelabel1
, it will not yet have been created.Changing the order of the declarations in the
XAML
(i.e., listinglabel1
beforecomboBox1
, ignoring issues of design philosophy) would at least resolve theNullReferenceException
here.Cast with
as
This doesn't throw an
InvalidCastException
but returns anull
when the cast fails (and whensomeObject
is itself null). So be aware of that.LINQ
FirstOrDefault()
andSingleOrDefault()
The plain versions
First()
andSingle()
throw exceptions when there is nothing. The "OrDefault" versions returnnull
in that case. So be aware of that.foreach
foreach
throws when you try to iterate on anull
collection. Usually caused by unexpectednull
result from methods that return collections.More realistic example - select nodes from XML document. Will throw if nodes are not found but initial debugging shows that all properties valid:
Ways to Avoid
Explicitly check for
null
and ignorenull
values.If you expect the reference sometimes to be
null
, you can check for it beingnull
before accessing instance members:Explicitly check for
null
and provide a default value.Methods you call expecting an instance can return
null
, for example when the object being sought cannot be found. You can choose to return a default value when this is the case:Explicitly check for
null
from method calls and throw a custom exception.You can also throw a custom exception, only to catch it in the calling code:
Use
Debug.Assert
if a value should never benull
, to catch the problem earlier than the exception occurs.When you know during development that a method could, but never should return
null
, you can useDebug.Assert()
to break as soon as possible when it does occur:Though this check will not end up in your release build, causing it to throw the
NullReferenceException
again whenbook == null
at runtime in release mode.Use
GetValueOrDefault()
fornullable
value types to provide a default value when they arenull
.Use the null coalescing operator:
??
[C#] orIf()
[VB].The shorthand to providing a default value when a
null
is encountered:Use the null condition operator:
?.
or?[x]
for arrays (available in C# 6 and VB.NET 14):This is also sometimes called the safe navigation or Elvis (after its shape) operator. If the expression on the left side of the operator is null, then the right side will not be evaluated, and null is returned instead. That means cases like this:
If the person does not have a title, this will throw an exception because it is trying to call
ToUpper
on a property with a null value.In
C# 5
and below, this can be guarded with:Now the title variable will be null instead of throwing an exception. C# 6 introduces a shorter syntax for this:
This will result in the title variable being
null
, and the call toToUpper
is not made ifperson.Title
isnull
.Of course, you still have to check
title
fornull
or use the null condition operator together with the null coalescing operator (??
) to supply a default value:Likewise, for arrays you can use
?[i]
as follows:This will do the following: If
myIntArray
isnull
, the expression returnsnull
and you can safely check it. If it contains an array, it will do the same as:elem = myIntArray[i];
and returns the ith element.Use null context (available in C# 8):
Introduced in
C# 8
, null contexts and nullable reference types perform static analysis on variables and provide a compiler warning if a value can be potentiallynull
or have been set tonull
. The nullable reference types allow types to be explicitly allowed to benull
.The nullable annotation context and nullable warning context can be set for a project using the
Nullable
element in yourcsproj
file. This element configures how the compiler interprets the nullability of types and what warnings are generated. Valid settings are:enable
: The nullable annotation context is enabled. The nullable warning context is enabled. Variables of a reference type, string, for example, are non-nullable. All nullability warnings are enabled.disable
: The nullable annotation context is disabled. The nullable warning context is disabled. Variables of a reference type are oblivious, just like earlier versions of C#. All nullability warnings are disabled.safeonly
: The nullable annotation context is enabled. The nullable warning context is safeonly. Variables of a reference type are non-nullable. All safety nullability warnings are enabled.warnings
: The nullable annotation context is disabled. The nullable warning context is enabled. Variables of a reference type are oblivious. All nullability warnings are enabled.safeonlywarnings
: The nullable annotation context is disabled. The nullable warning context is safeonly.Variables of a reference type are oblivious. All safety nullability warnings are enabled.
A nullable reference type is noted using the same syntax as nullable value types: a
?
is appended to the type of the variable.Special techniques for debugging and fixing null derefs in iterators
C#
supports "iterator blocks" (called "generators" in some other popular languages).NullReferenceException
can be particularly tricky to debug in iterator blocks because of deferred execution:If
whatever
results innull
thenMakeFrob
will throw. Now, you might think that the right thing to do is this:Why is this wrong? Because the iterator block does not actually run until the
foreach
! The call toGetFrobs
simply returns an object which when iterated will run the iterator block.By writing a
null
check like this you prevent theNullReferenceException
, but you move theNullArgumentException
to the point of the iteration, not to the point of the call, and that is very confusing to debug.The correct fix is:
That is, make a private helper method that has the iterator block logic and a public surface method that does the
null
check and returns the iterator. Now whenGetFrobs
is called, thenull
check happens immediately, and thenGetFrobsForReal
executes when the sequence is iterated.If you examine the reference source for
LINQ
to Objects you will see that this technique is used throughout. It is slightly more clunky to write, but it makes debugging nullity errors much easier. Optimize your code for the convenience of the caller, not the convenience of the author.A note on null dereferences in unsafe code
C#
has an "unsafe" mode which is, as the name implies, extremely dangerous because the normal safety mechanisms which provide memory safety and type safety are not enforced. You should not be writing unsafe code unless you have a thorough and deep understanding of how memory works.In unsafe mode, you should be aware of two important facts:
To understand why that is, it helps to understand how .NET produces
NullReferenceException
in the first place. (These details apply to .NET running on Windows; other operating systems use similar mechanisms.)Memory is virtualized in
Windows
; each process gets a virtual memory space of many "pages" of memory that are tracked by the operating system. Each page of memory has flags set on it that determine how it may be used: read from, written to, executed, and so on. The lowest page is marked as "produce an error if ever used in any way".Both a null pointer and a null reference in
C#
are internally represented as the number zero, and so any attempt to dereference it into its corresponding memory storage causes the operating system to produce an error. The .NET runtime then detects this error and turns it into theNullReferenceException
.That's why dereferencing both a null pointer and a null reference produces the same exception.
What about the second point? Dereferencing any invalid pointer that falls in the lowest page of virtual memory causes the same operating system error, and thereby the same exception.
Why does this make sense? Well, suppose we have a struct containing two ints, and an unmanaged pointer equal to null. If we attempt to dereference the second int in the struct, the
CLR
will not attempt to access the storage at location zero; it will access the storage at location four. But logically this is a null dereference because we are getting to that address via the null.If you are working with unsafe code and you get a
NullReferenceException
, just be aware that the offending pointer need not be null. It can be any location in the lowest page, and this exception will be produced.NullReference 异常 - Visual Basic
Visual Basic 的
NullReference 异常
与 C# 中的 NullReference 异常没有什么不同。毕竟,它们都报告它们都使用的 .NET Framework 中定义的相同异常。 Visual Basic 特有的原因很少(也许只有一个)。本答案将使用 Visual Basic 术语、语法和上下文。使用的例子来自大量过去的Stack溢出问题。这是为了通过使用帖子中常见的各种情况来最大化相关性。还为可能需要的人提供了更多解释。这里很可能列出了与您类似的示例。
注意:
基本含义
消息“对象未设置为对象的实例”意味着您正在尝试使用尚未初始化的对象。这可以归结为以下之一:
查找原因
由于问题是一个对象引用,它是
Nothing
,所以答案是检查它们以找出是哪一个。然后判断为什么没有初始化。将鼠标悬停在各个变量上,Visual Studio (VS) 将显示它们的值 - 罪魁祸首将是Nothing
。您还应该从相关代码中删除所有 Try/Catch 块,尤其是 Catch 中没有任何内容的代码块堵塞。当您的代码尝试使用
Nothing
的对象时,这将导致您的代码崩溃。 这就是您想要的,因为它将识别问题的确切位置,并允许您识别导致问题的对象。Catch 中显示
Error while...
的MsgBox
没有什么帮助。此方法还会导致堆栈非常糟糕溢出问题,因为您无法描述实际的异常、涉及的对象甚至发生异常的代码行。您还可以使用
Locals Window
(调试 -> Windows -> Locals)来检查您的对象。一旦您知道问题是什么以及问题出在哪里,通常就很容易修复,并且比发布新问题更快。
另请参阅:
示例和补救措施
类对象/创建实例
问题是
Dim
不会创建 CashRegister 对象;它仅声明该类型的名为reg
的变量。 声明对象变量和创建实例是两件不同的事情。补救措施
New
运算符通常可用于在声明实例时创建实例:当仅适合稍后创建实例时:
注意:不要< /strong> 在过程中再次使用
Dim
,包括构造函数 (Sub New
):这将创建一个本地变量,
reg
,仅存在于该上下文(子)中。您将在其他地方使用的具有模块级别Scope
的reg
变量仍然是Nothing
。需要明确的是,
Dim
(或Private
)仅声明一个变量及其类型
。变量的范围 - 无论它存在于整个模块/类还是局部于过程 - 由声明它的位置决定。私人 |朋友| Public
定义访问级别,而不是范围。有关详细信息,请参阅:
数组
数组还必须实例化:
该数组仅被声明,尚未创建。初始化数组的方法有多种:
注意:从 VS 2010 开始,当使用文字和
Option Infer
初始化本地数组时,As
和 < code>New 元素是可选的:数据类型和数组大小是根据分配的数据推断出来的。类/模块级别声明仍然需要
As
和Option Strict
:示例:类对象数组
数组已创建,但是其中的
Foo
对象还没有。补救措施
使用
List(Of T)
会使元素没有有效对象变得非常困难:有关详细信息,请参阅:
列表和集合
。 NET 集合(其中有很多种类 - 列表、字典等)也必须实例化或创建。
由于相同的原因,您会得到相同的异常 - 仅声明了 myList,但未创建实例。补救措施是相同的:
常见的疏忽是使用集合
Type
的类:任一过程都会导致 NRE,因为
barList
仅被声明,而非实例化。创建Foo
的实例不会同时创建内部barList
的实例。可能是想在构造函数中执行此操作:与以前一样,这是不正确的:
有关详细信息,请参阅
List(Of T)
类。数据提供者对象
使用数据库为 NullReference 提供了许多机会,因为可以有许多对象(
Command
、Connection
、Transaction
、Dataset
、DataTable
、DataRows
....) 立即使用。 注意:无论您使用哪个数据提供程序(MySQL、SQL Server、OleDB 等),概念都是相同的。示例 1
与之前一样,声明了
ds
Dataset 对象,但从未创建实例。DataAdapter
将填充现有的DataSet
,而不是创建一个。在这种情况下,由于 ds 是局部变量,因此 IDE 会警告您可能会发生这种情况:当声明为模块/类级别变量时,如
con
的情况,编译器无法知道该对象是否由上游过程创建。不要忽视警告。补救措施
示例 2
这里存在拼写错误的问题:
Employees
与Employee
。没有创建名为“Employee”的DataTable
,因此尝试访问它会导致NullReferenceException
。另一个潜在的问题是假设存在Items
,但当 SQL 包含 WHERE 子句时,情况可能并非如此。补救措施
由于这使用一个表,因此使用
Tables(0)
将避免拼写错误。检查Rows.Count
也有帮助:Fill
是一个返回受影响的Rows
数量的函数,也可以对其进行测试:示例 3
DataAdapter
将提供TableNames
,如上例所示,但它不会解析 SQL 或数据库表中的名称。因此,ds.Tables("TICKET_RESERVATION")
引用了一个不存在的表。补救措施是相同的,通过索引引用表格:
另请参阅DataTable 类。
对象路径/嵌套
代码仅测试
Items
,而myFoo
和Bar
也可能是 Nothing。 补救措施是一次测试一个对象的整个链或路径:AndAlso
很重要。一旦遇到第一个False
条件,后续测试将不会执行。这允许代码一次安全地“钻取”对象一个“级别”,仅在确定myFoo
后(并且如果)才评估myFoo.Bar
是有效的。在对复杂对象进行编码时,对象链或路径可能会变得相当长:不可能引用
null
对象的任何“下游”内容。这也适用于控件:这里,
myWebBrowser
或Document
可以为 Nothing,或者formfld1
元素可能不存在。UI 控件
除此之外,此代码并未预期用户可能未在一个或多个 UI 控件中选择某些内容。
ListBox1.SelectedItem
很可能是Nothing
,因此ListBox1.SelectedItem.ToString
将导致 NRE。补救措施
在使用数据之前验证数据(也使用
Option Strict
和SQL参数):或者,您可以使用
(ComboBox5.SelectedItem IsNot Nothing) AndAlso...< /code>
Visual Basic Forms
这是获取 NRE 的相当常见的方法。在 C# 中,根据编码方式,IDE 将报告当前上下文中不存在
Controls
,或“无法引用非静态成员”。所以,从某种程度上来说,这是一种仅限 VB 的情况。它也很复杂,因为它可能导致级联故障。数组和集合不能以这种方式初始化。此初始化代码将在构造函数创建
Form
或Controls.结果:
somevar
赋值将导致立即 NRE,因为 Nothing 没有.Text
property稍后引用数组元素将产生 NRE。如果您在
Form_Load
中执行此操作,由于奇怪的错误,IDE 在发生异常时可能不会报告该异常。当您的代码尝试使用该数组时,稍后会弹出异常。 这篇文章详细介绍了这种“静默异常”。就我们的目的而言,关键是当创建表单时发生灾难性事件(Sub New
或Form Load
事件)时,可能不会报告异常,代码会退出过程并仅显示表格。由于
Sub New
或Form Load
事件中的其他代码不会在 NRE 之后运行,因此许多其他内容可以保持未初始化。注意这适用于任何和所有控件和组件引用,使得这些引用在它们所在的地方都是非法的:
部分补救措施
奇怪的是 VB 没有提供警告,但补救措施是在表单级别声明容器,但当控件确实存在时,在表单加载事件处理程序中初始化它们。只要您的代码位于
InitializeComponent
调用之后,就可以在Sub New
中完成此操作:数组代码可能尚未脱离困境。容器控件(例如
GroupBox
或Panel
)中的任何控件都不会在Me.Controls
中找到;它们将位于该面板或组框的控件集合中。当控件名称拼写错误时 ("TeStBox2"
),也不会返回控件。在这种情况下,Nothing
将再次存储在这些数组元素中,并且当您尝试引用它时将产生 NRE。既然您知道自己在寻找什么,那么这些应该很容易找到:
“Button2”位于
面板
补救措施
不要使用表单的
Controls
集合按名称间接引用,而是使用控件引用:函数不返回任何内容
在这种情况下,IDE 会警告您“并非所有路径都返回”可能会导致一个值和一个
NullReferenceException
'。您可以通过将Exit Function
替换为Return Nothing
来抑制警告,但这并不能解决问题。当someCondition = False
时尝试使用 return 的任何操作都会导致 NRE:补救措施
将函数中的
Exit Function
替换为Return b列表
。返回空List
与返回Nothing
不同。如果返回的对象有可能Nothing
,请在使用它之前进行测试:实施不当的 Try/Catch
实施不当的 Try/Catch 可能会隐藏问题所在并导致新的问题:
这是未按预期创建对象的情况,但也演示了空
Catch
的计数器用途。SQL 中有一个额外的逗号(在“mailaddress”之后),这会导致
.ExecuteReader
出现异常。在Catch
不执行任何操作后,Finally
尝试执行清理,但由于您无法Close
空DataReader
对象,一个全新的NullReferenceException
结果。空的
Catch
块是魔鬼的游乐场。这位 OP 很困惑为什么他会在Finally
块中得到一个 NRE。在其他情况下,空的Catch
可能会导致下游的其他事情变得混乱,并导致您花时间在错误的位置查找错误的内容来解决问题。 (上述“静默异常”提供了相同的娱乐价值。)补救措施
不要使用空的 Try/Catch 块 - 让代码崩溃,以便您可以 a) 确定原因 b) 确定位置c) 采取适当的补救措施。 Try/Catch 块并不是为了向唯一有资格修复它们的人(开发人员)隐藏异常。
DBNull 与 Nothing 不同
IsDBNull
函数用于测试值是否等于System.DBNull
:来自 MSDN:补救措施
与之前一样,您可以测试 Nothing,然后测试特定值:
示例 2
FirstOrDefault
返回第一项或默认值,其中对于引用类型来说是Nothing
,并且从不DBNull
:控制
如果无法找到带有
chkName
的CheckBox
(或者存在于GroupBox
中),则chk
将为 Nothing,并且尝试引用任何属性都会导致异常。补救措施
DataGridView
DGV 有一些定期出现的怪癖:
如果
dgvBooks
具有AutoGenerateColumns = True
,它将创建列,但不会命名它们,因此上面的代码在按名称引用它们时会失败。补救措施
手动命名列,或按索引引用:
请注意 NewRow
示例 2 -当您的
DataGridView
将AllowUserToAddRows
设置为True< 时, /code> (默认),底部空白/新行中的
Cells
将全部包含Nothing
。大多数使用内容的尝试(例如,ToString
)都会导致 NRE。补救措施
使用
For/Each
循环并测试IsNewRow
属性以确定它是否是最后一行。无论AllowUserToAddRows
是否为真,这都有效:如果您确实使用
For n
循环,请修改行数或在Exit For
时使用>IsNewRow 为 true。My.Settings (StringCollection)
在某些情况下,尝试使用
My.Settings
中的一个StringCollection
项目可能会在您第一次使用它时导致 NullReference。解决方案是相同的,但不那么明显。考虑一下:由于 VB 正在为您管理“设置”,因此期望它初始化该集合是合理的。它会,但前提是您之前已将初始条目添加到集合中(在“设置”编辑器中)。由于该集合(显然)是在添加项目时初始化的,因此当“设置”编辑器中没有要添加的项目时,该集合将保持
Nothing
状态。补救措施
如果需要的话,在表单的
Load
事件处理程序中初始化设置集合:通常,
Settings
集合只需要在以下情况下初始化:应用程序第一次运行时。另一种补救措施是在项目 -> 中为您的集合添加初始值。设置 | FooBars,保存项目,然后删除假值。要点
您可能忘记了
New
运算符。或者
您认为可以完美地执行以将初始化的对象返回到您的代码的某些操作,但事实并非如此。
不要忽略编译器警告(永远)并使用
Option Strict On
(始终)。MSDN NullReference 异常
NullReference Exception — Visual Basic
The
NullReference Exception
for Visual Basic is no different from the one in C#. After all, they are both reporting the same exception defined in the .NET Framework which they both use. Causes unique to Visual Basic are rare (perhaps only one).This answer will use Visual Basic terms, syntax, and context. The examples used come from a large number of past Stack Overflow questions. This is to maximize relevance by using the kinds of situations often seen in posts. A bit more explanation is also provided for those who might need it. An example similar to yours is very likely listed here.
Note:
NullReferenceException
(NRE), how to find it, how to fix it, and how to avoid it. An NRE can be caused many ways so this is unlikely to be your sole encounter.Basic Meaning
The message "Object not set to an instance of Object" means you are trying to use an object which has not been initialized. This boils down to one of these:
Finding The Cause
Since the problem is an object reference which is
Nothing
, the answer is to examine them to find out which one. Then determine why it is not initialized. Hold the mouse over the various variables and Visual Studio (VS) will show their values - the culprit will beNothing
.You should also remove any Try/Catch blocks from the relevant code, especially ones where there is nothing in the Catch block. This will cause your code to crash when it tries to use an object which is
Nothing
. This is what you want because it will identify the exact location of the problem, and allow you to identify the object causing it.A
MsgBox
in the Catch which displaysError while...
will be of little help. This method also leads to very bad Stack Overflow questions, because you can't describe the actual exception, the object involved or even the line of code where it happens.You can also use the
Locals Window
(Debug -> Windows -> Locals) to examine your objects.Once you know what and where the problem is, it is usually fairly easy to fix and faster than posting a new question.
See also:
Examples and Remedies
Class Objects / Creating an Instance
The problem is that
Dim
does not create a CashRegister object; it only declares a variable namedreg
of that Type. Declaring an object variable and creating an instance are two different things.Remedy
The
New
operator can often be used to create the instance when you declare it:When it is only appropriate to create the instance later:
Note: Do not use
Dim
again in a procedure, including the constructor (Sub New
):This will create a local variable,
reg
, which exists only in that context (sub). Thereg
variable with module levelScope
which you will use everywhere else remainsNothing
.To be clear,
Dim
(orPrivate
) only declares a variable and itsType
. The Scope of the variable - whether it exists for the entire module/class or is local to a procedure - is determined by where it is declared.Private | Friend | Public
defines the access level, not Scope.For more information, see:
Arrays
Arrays must also be instantiated:
This array has only been declared, not created. There are several ways to initialize an array:
Note: Beginning with VS 2010, when initializing a local array using a literal and
Option Infer
, theAs <Type>
andNew
elements are optional:The data Type and array size are inferred from the data being assigned. Class/Module level declarations still require
As <Type>
withOption Strict
:Example: Array of class objects
The array has been created, but the
Foo
objects in it have not.Remedy
Using a
List(Of T)
will make it quite difficult to have an element without a valid object:For more information, see:
Lists and Collections
.NET collections (of which there are many varieties - Lists, Dictionary, etc.) must also be instantiated or created.
You get the same exception for the same reason -
myList
was only declared, but no instance created. The remedy is the same:A common oversight is a class which uses a collection
Type
:Either procedure will result in an NRE, because
barList
is only declared, not instantiated. Creating an instance ofFoo
will not also create an instance of the internalbarList
. It may have been the intent to do this in the constructor:As before, this is incorrect:
For more information, see
List(Of T)
Class.Data Provider Objects
Working with databases presents many opportunities for a NullReference because there can be many objects (
Command
,Connection
,Transaction
,Dataset
,DataTable
,DataRows
....) in use at once. Note: It does not matter which data provider you are using -- MySQL, SQL Server, OleDB, etc. -- the concepts are the same.Example 1
As before, the
ds
Dataset object was declared, but an instance was never created. TheDataAdapter
will fill an existingDataSet
, not create one. In this case, sinceds
is a local variable, the IDE warns you that this might happen:When declared as a module/class level variable, as appears to be the case with
con
, the compiler can't know if the object was created by an upstream procedure. Do not ignore warnings.Remedy
Example 2
A typo is a problem here:
Employees
vsEmployee
. There was noDataTable
named "Employee" created, so aNullReferenceException
results trying to access it. Another potential problem is assuming there will beItems
which may not be so when the SQL includes a WHERE clause.Remedy
Since this uses one table, using
Tables(0)
will avoid spelling errors. ExaminingRows.Count
can also help:Fill
is a function returning the number ofRows
affected which can also be tested:Example 3
The
DataAdapter
will provideTableNames
as shown in the previous example, but it does not parse names from the SQL or database table. As a result,ds.Tables("TICKET_RESERVATION")
references a non-existent table.The Remedy is the same, reference the table by index:
See also DataTable Class.
Object Paths / Nested
The code is only testing
Items
while bothmyFoo
andBar
may also be Nothing. The remedy is to test the entire chain or path of objects one at a time:AndAlso
is important. Subsequent tests will not be performed once the firstFalse
condition is encountered. This allows the code to safely 'drill' into the object(s) one 'level' at a time, evaluatingmyFoo.Bar
only after (and if)myFoo
is determined to be valid. Object chains or paths can get quite long when coding complex objects:It is not possible to reference anything 'downstream' of a
null
object. This also applies to controls:Here,
myWebBrowser
orDocument
could be Nothing or theformfld1
element may not exist.UI Controls
Among other things, this code does not anticipate that the user may not have selected something in one or more UI controls.
ListBox1.SelectedItem
may well beNothing
, soListBox1.SelectedItem.ToString
will result in an NRE.Remedy
Validate data before using it (also use
Option Strict
and SQL parameters):Alternatively, you can use
(ComboBox5.SelectedItem IsNot Nothing) AndAlso...
Visual Basic Forms
This is a fairly common way to get an NRE. In C#, depending on how it is coded, the IDE will report that
Controls
does not exist in the current context, or "cannot reference non-static member". So, to some extent, this is a VB-only situation. It is also complex because it can result in a failure cascade.The arrays and collections cannot be initialized this way. This initialization code will run before the constructor creates the
Form
or theControls
. As a result:somevar
assignment will result in an immediate NRE because Nothing doesn't have a.Text
propertyReferencing array elements later will result in an NRE. If you do this in
Form_Load
, due to an odd bug, the IDE may not report the exception when it happens. The exception will pop up later when your code tries to use the array. This "silent exception" is detailed in this post. For our purposes, the key is that when something catastrophic happens while creating a form (Sub New
orForm Load
event), exceptions may go unreported, the code exits the procedure and just displays the form.Since no other code in your
Sub New
orForm Load
event will run after the NRE, a great many other things can be left uninitialized.Note this applies to any and all control and component references making these illegal where they are:
Partial Remedy
It is curious that VB does not provide a warning, but the remedy is to declare the containers at the form level, but initialize them in form load event handler when the controls do exist. This can be done in
Sub New
as long as your code is after theInitializeComponent
call:The array code may not be out of the woods yet. Any controls which are in a container control (like a
GroupBox
orPanel
) will not be found inMe.Controls
; they will be in the Controls collection of that Panel or GroupBox. Nor will a control be returned when the control name is misspelled ("TeStBox2"
). In such cases,Nothing
will again be stored in those array elements and an NRE will result when you attempt to reference it.These should be easy to find now that you know what you are looking for:
"Button2" resides on a
Panel
Remedy
Rather than indirect references by name using the form's
Controls
collection, use the control reference:Function Returning Nothing
This is a case where the IDE will warn you that 'not all paths return a value and a
NullReferenceException
may result'. You can suppress the warning, by replacingExit Function
withReturn Nothing
, but that does not solve the problem. Anything which tries to use the return whensomeCondition = False
will result in an NRE:Remedy
Replace
Exit Function
in the function withReturn bList
. Returning an emptyList
is not the same as returningNothing
. If there is a chance that a returned object can beNothing
, test before using it:Poorly Implemented Try/Catch
A badly implemented Try/Catch can hide where the problem is and result in new ones:
This is a case of an object not being created as expected, but also demonstrates the counter usefulness of an empty
Catch
.There is an extra comma in the SQL (after 'mailaddress') which results in an exception at
.ExecuteReader
. After theCatch
does nothing,Finally
tries to perform clean up, but since you cannotClose
a nullDataReader
object, a brand newNullReferenceException
results.An empty
Catch
block is the devil's playground. This OP was baffled why he was getting an NRE in theFinally
block. In other situations, an emptyCatch
may result in something else much further downstream going haywire and cause you to spend time looking at the wrong things in the wrong place for the problem. (The "silent exception" described above provides the same entertainment value.)Remedy
Don't use empty Try/Catch blocks - let the code crash so you can a) identify the cause b) identify the location and c) apply a proper remedy. Try/Catch blocks are not intended to hide exceptions from the person uniquely qualified to fix them - the developer.
DBNull is not the same as Nothing
The
IsDBNull
function is used to test if a value equalsSystem.DBNull
: From MSDN:Remedy
As before, you can test for Nothing, then for a specific value:
Example 2
FirstOrDefault
returns the first item or the default value, which isNothing
for reference types and neverDBNull
:Controls
If a
CheckBox
withchkName
can't be found (or exists in aGroupBox
), thenchk
will be Nothing and be attempting to reference any property will result in an exception.Remedy
The DataGridView
The DGV has a few quirks seen periodically:
If
dgvBooks
hasAutoGenerateColumns = True
, it will create the columns, but it does not name them, so the above code fails when it references them by name.Remedy
Name the columns manually, or reference by index:
Example 2 — Beware of the NewRow
When your
DataGridView
hasAllowUserToAddRows
asTrue
(the default), theCells
in the blank/new row at the bottom will all containNothing
. Most attempts to use the contents (for example,ToString
) will result in an NRE.Remedy
Use a
For/Each
loop and test theIsNewRow
property to determine if it is that last row. This works whetherAllowUserToAddRows
is true or not:If you do use a
For n
loop, modify the row count or useExit For
whenIsNewRow
is true.My.Settings (StringCollection)
Under certain circumstances, trying to use an item from
My.Settings
which is aStringCollection
can result in a NullReference the first time you use it. The solution is the same, but not as obvious. Consider:Since VB is managing Settings for you, it is reasonable to expect it to initialize the collection. It will, but only if you have previously added an initial entry to the collection (in the Settings editor). Since the collection is (apparently) initialized when an item is added, it remains
Nothing
when there are no items in the Settings editor to add.Remedy
Initialize the settings collection in the form's
Load
event handler, if/when needed:Typically, the
Settings
collection will only need to be initialized the first time the application runs. An alternate remedy is to add an initial value to your collection in Project -> Settings | FooBars, save the project, then remove the fake value.Key Points
You probably forgot the
New
operator.or
Something you assumed would perform flawlessly to return an initialized object to your code, did not.
Don't ignore compiler warnings (ever) and use
Option Strict On
(always).MSDN NullReference Exception
另一种情况是当您将 null 对象转换为 值类型。例如,下面的代码:
它将在强制转换时抛出
NullReferenceException
。在上面的示例中这似乎很明显,但是这可能发生在更“后期绑定”的复杂场景中,其中空对象已从您不拥有的某些代码返回,并且强制转换是由某些自动系统生成的。其中一个示例是这个简单的 ASP.NET 与 Calendar 控件的绑定片段:
这里,
SelectedDate
实际上是Calendar 的一个
Web Control 类型,并且绑定可以完美地返回 null。隐式 ASP.NET 生成器将创建一段与上面的强制转换代码等效的代码。这将引发一个很难发现的DateTime
类型的属性NullReferenceException
,因为它位于 ASP.NET 生成的代码中,可以正常编译...Another scenario is when you cast a null object into a value type. For example, the code below:
It will throw a
NullReferenceException
on the cast. It seems quite obvious in the above sample, but this can happen in more "late-binding" intricate scenarios where the null object has been returned from some code you don't own, and the cast is for example generated by some automatic system.One example of this is this simple ASP.NET binding fragment with the Calendar control:
Here,
SelectedDate
is in fact a property - ofDateTime
type - of theCalendar
Web Control type, and the binding could perfectly return something null. The implicit ASP.NET Generator will create a piece of code that will be equivalent to the cast code above. And this will raise aNullReferenceException
that is quite difficult to spot, because it lies in ASP.NET generated code which compiles fine...这意味着您的代码使用了一个设置为 null 的对象引用变量(即它没有引用实际的对象实例)。
为了防止该错误,可能为 null 的对象应在使用之前测试是否为 null。
It means your code used an object reference variable that was set to null (i.e. it did not reference an actual object instance).
To prevent the error, objects that could be null should be tested for null before being used.
这意味着所讨论的变量没有指向任何内容。我可以这样生成:
这会抛出错误,因为虽然我声明了变量“
connection
”,但它没有指向任何内容。当我尝试调用成员“Open
”时,没有可供解析的引用,并且会抛出错误。要避免此错误:
object == null
进行检查。JetBrains 的 ReSharper 工具将识别代码中可能存在空引用的每个位置错误,允许您进行空检查。恕我直言,这个错误是错误的第一大来源。
It means that the variable in question is pointed at nothing. I could generate this like so:
That will throw the error because while I've declared the variable "
connection
", it's not pointed to anything. When I try to call the member "Open
", there's no reference for it to resolve, and it will throw the error.To avoid this error:
object == null
.JetBrains' ReSharper tool will identify every place in your code that has the possibility of a null reference error, allowing you to put in a null check. This error is the number one source of bugs, IMHO.
请注意,无论何种情况,.NET 中的原因始终相同:
Be aware that regardless of the scenario, the cause is always the same in .NET:
抛出此异常的一个示例是:当您尝试检查某些内容时,该内容为空。
例如:
当您尝试对尚未实例化的内容(即上面的代码)执行操作时,.NET 运行时将抛出 NullReferenceException。
与 ArgumentNullException 相比,如果方法期望传递给它的内容不为空,则通常将抛出 ArgumentNullException 作为防御措施。
更多信息请参见C# NullReferenceException 和 Null 参数。
An example of this exception being thrown is: When you are trying to check something, that is null.
For example:
The .NET runtime will throw a NullReferenceException when you attempt to perform an action on something which hasn't been instantiated i.e. the code above.
In comparison to an ArgumentNullException which is typically thrown as a defensive measure if a method expects that what is being passed to it is not null.
More information is in C# NullReferenceException and Null Parameter.
更新 C#8.0,2019:可为空引用类型
C#8.0 引入了可为空引用类型和不可为空引用类型。因此,必须仅检查可为 null 的引用类型以避免 NullReferenceException。
如果您尚未初始化引用类型,并且想要设置或读取其属性之一,则会抛出 NullReferenceException。
示例:
您可以通过检查变量是否不为空来简单地避免这种情况:
要完全理解为什么抛出 NullReferenceException,了解 值类型 和 [引用类型][3]。
因此,如果您正在处理值类型,则可能不会发生 NullReferenceException。不过在处理引用类型时您需要保持警惕!
顾名思义,只有引用类型可以保存引用或实际上指向任何内容(或“空”)。而值类型总是包含一个值。
引用类型(必须勾选这些):
值类型(可以忽略这些):
Update C#8.0, 2019: Nullable reference types
C#8.0 introduces nullable reference types and non-nullable reference types. So only nullable reference types must be checked to avoid a NullReferenceException.
If you have not initialized a reference type, and you want to set or read one of its properties, it will throw a NullReferenceException.
Example:
You can simply avoid this by checking if the variable is not null:
To fully understand why a NullReferenceException is thrown, it is important to know the difference between value types and [reference types][3].
So, if you're dealing with value types, NullReferenceExceptions can not occur. Though you need to keep alert when dealing with reference types!
Only reference types, as the name is suggesting, can hold references or point literally to nothing (or 'null'). Whereas value types always contain a value.
Reference types (these ones must be checked):
Value types (you can simply ignore these ones):
可能发生 NullReferenceExceptions 的另一种情况是(不正确)使用
as
运算符:这里,
Book
和Car
是不兼容的类型;汽车
无法转换/转换为书籍
。当此转换失败时,as
返回null
。此后使用mybook
会导致NullReferenceException
。一般来说,您应该使用强制转换或
as
,如下所示:如果您期望类型转换始终成功(即您提前知道对象应该是什么),那么您应该使用a 强制转换:
如果您不确定类型,但想要尝试将其用作特定类型,则使用
as
:Another case where
NullReferenceExceptions
can happen is the (incorrect) use of theas
operator:Here,
Book
andCar
are incompatible types; aCar
cannot be converted/cast to aBook
. When this cast fails,as
returnsnull
. Usingmybook
after this causes aNullReferenceException
.In general, you should use a cast or
as
, as follows:If you are expecting the type conversion to always succeed (ie. you know what the object should be ahead of time), then you should use a cast:
If you are unsure of the type, but you want to try to use it as a specific type, then use
as
:您正在使用包含空值引用的对象。所以它给出了一个空异常。在示例中,字符串值为 null,在检查其长度时发生异常。
示例:
异常错误为:
You are using the object that contains the null value reference. So it's giving a null exception. In the example the string value is null and when checking its length, the exception occurred.
Example:
The exception error is:
而什么导致NullReferenceExceptions 以及避免/修复此类异常的方法已在其他答案中得到解决,许多程序员尚未学会的是如何独立调试此类异常开发过程中出现异常。
在 Visual Studio 中,借助 Visual Studio 调试器,这通常很容易。
首先,确保将捕获正确的错误 - 请参阅
如何允许在 VS2010 中中断“System.NullReferenceException”? 注意1< /em>
然后 < em>从调试开始 (F5) 或 将[VS 调试器]附加到正在运行的进程。有时使用
Debugger.Break
,这将提示启动调试器。现在,当抛出(或未处理)NullReferenceException 时,调试器将在发生异常的行上停止(还记得上面设置的规则吗?)。有时错误很容易被发现。
例如,
在下面的行中,可能导致异常的唯一代码是如果
myString
计算结果为 null。这可以通过查看观察窗口 或在立即窗口中运行表达式。在更高级的情况下,例如以下情况,您需要使用上述技术之一(Watch 或 Immediate Windows)来检查表达式以确定
str1
是否为 null 或者str2
为空。一旦定位了抛出异常的位置,通常很容易向后推理以找出[错误]引入空值的位置 -
花一些时间来了解异常的原因。检查空表达式。检查可能导致此类空表达式的先前表达式。添加断点 并根据需要逐步执行该程序。 使用调试器。
1 如果 Break on Throws 过于激进并且调试器在 .NET 或第 3 方库中的 NPE 上停止,Break on User-Unhandled 可用于限制捕获的异常。此外,VS2012 引入了Just My Code,我也建议启用它。
While what causes a NullReferenceExceptions and approaches to avoid/fix such an exception have been addressed in other answers, what many programmers haven't learned yet is how to independently debug such exceptions during development.
In Visual Studio this is usually easy thanks to the Visual Studio Debugger.
First, make sure that the correct error is going to be caught - see
How do I allow breaking on 'System.NullReferenceException' in VS2010? Note1
Then either Start with Debugging (F5) or Attach [the VS Debugger] to Running Process. On occasion it may be useful to use
Debugger.Break
, which will prompt to launch the debugger.Now, when the NullReferenceException is thrown (or unhandled) the debugger will stop (remember the rule set above?) on the line on which the exception occurred. Sometimes the error will be easy to spot.
For instance,
in the following line the only code that can cause the exception is if
myString
evaluates to null. This can be verified by looking at the Watch Window or running expressions in the Immediate Window.In more advanced cases, such as the following, you'll need to use one of the techniques above (Watch or Immediate Windows) to inspect the expressions to determine if
str1
was null or ifstr2
was null.Once where the exception is throw has been located, it's usually trivial to reason backwards to find out where the null value was [incorrectly] introduced --
Take the time required to understand the cause of the exception. Inspect for null expressions. Inspect the previous expressions which could have resulted in such null expressions. Add breakpoints and step through the program as appropriate. Use the debugger.
1 If Break on Throws is too aggressive and the debugger stops on an NPE in the .NET or 3rd-party library, Break on User-Unhandled can be used to limit the exceptions caught. Additionally, VS2012 introduces Just My Code which I recommend enabling as well.
Simon Mourier 给出了这个示例:
其中拆箱转换(强制转换) 来自
object
(或来自System.ValueType
或System.Enum
类之一,或来自接口type)值类型(Nullable<>
除外)本身会产生NullReferenceException
。另一方面,从具有
HasValue
Nullable
的装箱转换 code> 等于false
对于引用类型,可以给出null
引用,该引用随后会导致NullReferenceException
。经典的例子是:有时拳击以另一种方式发生。例如,使用此非泛型扩展方法:
以下代码将出现问题:
这些情况的出现是由于运行时在装箱
Nullable
实例时使用的特殊规则。Simon Mourier gave this example:
where an unboxing conversion (cast) from
object
(or from one of the classesSystem.ValueType
orSystem.Enum
, or from an interface type) to a value type (other thanNullable<>
) in itself gives theNullReferenceException
.In the other direction, a boxing conversion from a
Nullable<>
which hasHasValue
equal tofalse
to a reference type, can give anull
reference which can then later lead to aNullReferenceException
. The classic example is:Sometimes the boxing happens in another way. For example with this non-generic extension method:
the following code will be problematic:
These cases arise because of the special rules the runtime uses when boxing
Nullable<>
instances.添加当实体框架中使用的实体的类名与 Web 表单代码隐藏文件的类名相同时的情况。
假设您有一个 Web 表单 Contact.aspx,其代码隐藏类是 Contact,并且您有一个实体名称 Contact。
然后,当您调用 context.SaveChanges() 时,以下代码将抛出 NullReferenceException
。为了完整性,DataContext 类
和 Contact 实体类。有时实体类是部分类,因此您也可以在其他文件中扩展它们。
当实体和代码隐藏类位于同一命名空间中时,会发生错误。
要解决此问题,请重命名 Contact.aspx 的实体类或代码隐藏类。
原因
我仍然不确定原因。但每当任何实体类扩展 System.Web.UI.Page 时,就会发生此错误。
有关讨论,请查看 DbContext.saveChanges() 中的 NullReferenceException
Adding a case when the class name for entity used in entity framework is same as class name for a web form code-behind file.
Suppose you have a web form Contact.aspx whose codebehind class is Contact and you have an entity name Contact.
Then following code will throw a NullReferenceException when you call context.SaveChanges()
For the sake of completeness DataContext class
and Contact entity class. Sometimes entity classes are partial classes so that you can extend them in other files too.
The error occurs when both the entity and codebehind class are in same namespace.
To fix this, rename the entity class or the codebehind class for Contact.aspx.
Reason
I am still not sure about the reason. But whenever any of the entity class will extend System.Web.UI.Page this error occurs.
For discussion have a look at NullReferenceException in DbContext.saveChanges()
另一种可能收到此异常的常见情况涉及在单元测试期间模拟类。无论使用哪种模拟框架,您都必须确保正确模拟类层次结构的所有适当级别。特别是,被测试代码引用的 HttpContext 的所有属性都必须被模拟。
请参阅“测试自定义 AuthorizationAttribute 时抛出 NullReferenceException”以获得稍微详细的示例。
Another general case where one might receive this exception involves mocking classes during unit testing. Regardless of the mocking framework being used, you must ensure that all appropriate levels of the class hierarchy are properly mocked. In particular, all properties of
HttpContext
which are referenced by the code under test must be mocked.See "NullReferenceException thrown when testing custom AuthorizationAttribute" for a somewhat verbose example.
我对这个问题有不同的看法。这种答案“我还能做什么来避免它?”
当跨不同层工作时,例如在 MVC 应用程序中,控制器需要服务来调用业务操作。在这种情况下,可以使用依赖注入容器来初始化服务以避免NullReferenceException。因此,这意味着您无需担心检查 null,只需从控制器调用服务,就好像它们始终作为单例或原型可用(并初始化)一样。
I have a different perspective to answering this. This sort of answers "what else can I do to avoid it?"
When working across different layers, for example in an MVC application, a controller needs services to call business operations. In such scenarios Dependency Injection Container can be used to initialize the services to avoid the NullReferenceException. So that means you don't need to worry about checking for null and just call the services from the controller as though they will always to available (and initialized) as either a singleton or a prototype.
关于“我该怎么办”这个问题,可以有很多答案。
在开发时防止此类错误情况的一种更“正式”的方法是应用在代码中按契约设计。这意味着您需要在开发时在系统上设置类不变量,和/或什至函数/方法前置条件和后置条件。
简而言之,类不变量确保您的类中存在一些在正常使用中不会被违反的约束(因此,该类不会处于不一致的状态)。 先决条件意味着作为函数/方法的输入给出的数据必须遵循一些约束集并且决不违反它们,而后置条件意味着函数/方法输出必须再次遵循设定的约束,而不能违反它们。
在执行无错误程序期间绝不违反合同条件,因此在调试模式下实际上会检查合同设计,同时在发布中禁用,以最大限度地提高开发的系统性能。
这样,您可以避免因违反约束集而导致的
NullReferenceException
情况。例如,如果您在类中使用对象属性X
,然后尝试调用其方法之一,而X
具有 null 值,则这将导致NullReferenceException
:但是,如果您将“属性 X 绝不能有空值”设置为方法前提条件,那么您可以防止前面描述的情况:
为此,代码契约项目存在于.NET应用程序中。
或者,可以使用断言来应用按合同设计。
更新:值得一提的是,该术语是由 Bertrand Meyer 创造的 与他设计的 Eiffel 编程语言有关。
On the matter of "what should I do about it", there can be many answers.
A more "formal" way of preventing such error conditions while developing is applying design by contract in your code. This means you need to set class invariants, and/or even function/method preconditions and postconditions on your system, while developing.
In short, class invariants ensure that there will be some constraints in your class that will not get violated in normal use (and therefore, the class will not get in an inconsistent state). Preconditions mean that data given as input to a function/method must follow some constraints set and never violate them, and postconditions mean that a function/method output must follow the set constraints again without ever violating them.
Contract conditions should never be violated during execution of a bug-free program, therefore design by contract is checked in practice in debug mode, while being disabled in releases, to maximize the developed system performance.
This way, you can avoid
NullReferenceException
cases that are results of violation of the constraints set. For example, if you use an object propertyX
in a class and later try to invoke one of its methods andX
has a null value, then this will lead toNullReferenceException
:But if you set "property X must never have a null value" as method precondition, then you can prevent the scenario described before:
For this cause, Code Contracts project exists for .NET applications.
Alternatively, design by contract can be applied using assertions.
UPDATE: It is worth mentioning that the term was coined by Bertrand Meyer in connection with his design of the Eiffel programming language.
当我们尝试访问 null 对象的 Properties 时,或者当字符串值变为空并且我们尝试访问字符串方法时,会引发 NullReferenceException。
例如:
当访问空字符串的字符串方法时:
当访问空对象的属性时:
A
NullReferenceException
is thrown when we are trying to access Properties of a null object or when a string value becomes empty and we are trying to access string methods.For example:
When a string method of an empty string accessed:
When a property of a null object accessed:
TL;DR:尝试使用
Html.Partial
而不是Renderpage
我得到
对象引用未设置到对象的实例< /code> 当我尝试通过向视图发送模型来渲染视图中的视图时,如下所示:
调试显示模型在 MyOtherView 中为 Null。直到我将其更改为:
并且它起作用了。
此外,我一开始就没有
Html.Partial
的原因是 Visual Studio 有时 在Html.Partial
下抛出看起来错误的波浪线> 如果它位于不同构造的foreach
循环内,即使它并不是真正的错误:但我能够运行该应用程序,而不会出现此“错误”的问题。我能够通过将 foreach 循环的结构更改为如下所示来消除该错误:
虽然我有一种感觉,这是因为 Visual Studio 误读了 & 符号和括号。
TL;DR: Try using
Html.Partial
instead ofRenderpage
I was getting
Object reference not set to an instance of an object
when I tried to render a View within a View by sending it a Model, like this:Debugging showed the model was Null inside MyOtherView. Until I changed it to:
And it worked.
Furthermore, the reason I didn't have
Html.Partial
to begin with was because Visual Studio sometimes throws error-looking squiggly lines underHtml.Partial
if it's inside a differently constructedforeach
loop, even though it's not really an error:But I was able to run the application with no problems with this "error". I was able to get rid of the error by changing the structure of the
foreach
loop to look like this:Although I have a feeling it was because Visual Studio was misreading the ampersands and brackets.
你能对此做什么?
这里有很多很好的答案,解释了什么是空引用以及如何调试它。但关于如何预防该问题或至少使其更容易被发现的信息却很少。
检查参数
例如,方法可以检查不同的参数以查看它们是否为 null 并抛出
ArgumentNullException
,显然是为此目的而创建的异常。ArgumentNullException
的构造函数甚至将参数名称和消息作为参数,以便您可以准确地告诉开发人员问题所在。使用工具
还有一些库可以提供帮助。例如,“Resharper”可以在您编写代码时向您提供警告,特别是在您使用其属性时: NotNullAttribute
有“Microsoft 代码合同”,您可以在其中使用
Contract.Requires(obj != null)
等语法,为您提供运行时和编译检查:介绍代码契约。还有“PostSharp”,它允许您仅使用如下属性:
通过这样做并使 PostSharp 成为构建过程的一部分,将在运行时检查
obj
是否为 null。请参阅:PostSharp 空值检查纯代码解决方案
或者您始终可以使用纯旧代码编写自己的方法。例如,这是一个可用于捕获空引用的结构。它采用与
Nullable
相同的概念进行建模:您的使用方式与使用
Nullable
的方式非常相似,只不过目标是准确完成相反 - 不允许null
。以下是一些示例:NotNull
与T
之间隐式转换,因此您可以在任何需要的地方使用它。例如,您可以将Person
对象传递给采用NotNull
的方法:正如您在上面所看到的,对于可为 null 的情况,您可以通过
Value
属性。或者,您可以使用显式或隐式转换,您可以看到下面带有返回值的示例:或者您甚至可以在方法仅返回
T
时使用它(在本例中为Person< /code>) 通过进行强制转换。例如,以下代码与上面的代码类似:
与扩展结合
将
NotNull
与扩展方法结合,您可以覆盖更多情况。以下是扩展方法的示例:以下是如何使用它的示例:
GitHub
为了供您参考,我在 GitHub 上提供了上述代码,您可以在以下位置找到它:
https://github.com/luisperezphd/NotNull
相关语言功能
C# 6.0 引入了“null-conditional”运算符”这对此有一点帮助。利用此功能,您可以引用嵌套对象,如果其中任何一个为
null
,则整个表达式将返回null
。这减少了在某些情况下必须执行的空检查的数量。语法是在每个点之前放置一个问号。以下面的代码为例:
假设
country
是一个Country
类型的对象,它有一个名为State
的属性等等。如果country
、State
、County
或City
为null
则地址将为
null。因此,您只需检查
address是否为
null`。这是一个很棒的功能,但它为您提供的信息较少。它并没有明确表明这 4 个中哪一个为空。
像 Nullable 一样内置?
C# 有一个很好的
Nullable
简写方式,您可以通过在类型后面添加一个问号来使某些内容可以为 null,如int ?
。如果 C# 具有类似于上面的
NotNull
结构的内容,并且具有类似的简写形式(可能是感叹号 (!)),那就太好了,这样您就可以编写如下内容:public void WriteName (人!人)
。What can you do about it?
There is a lot of good answers here explaining what a null reference is and how to debug it. But there is very little on how to prevent the issue or at least make it easier to catch.
Check arguments
For example, methods can check the different arguments to see if they are null and throw an
ArgumentNullException
, an exception obviously created for this exact purpose.The constructor for the
ArgumentNullException
even takes the name of the parameter and a message as arguments so you can tell the developer exactly what the problem is.Use Tools
There are also several libraries that can help. "Resharper" for example can provide you with warnings while you are writing code, especially if you use their attribute: NotNullAttribute
There's "Microsoft Code Contracts" where you use syntax like
Contract.Requires(obj != null)
which gives you runtime and compile checking: Introducing Code Contracts.There's also "PostSharp" which will allow you to just use attributes like this:
By doing that and making PostSharp part of your build process
obj
will be checked for null at runtime. See: PostSharp null checkPlain Code Solution
Or you can always code your own approach using plain old code. For example here is a struct that you can use to catch null references. It's modeled after the same concept as
Nullable<T>
:You would use very similar to the same way you would use
Nullable<T>
, except with the goal of accomplishing exactly the opposite - to not allownull
. Here are some examples:NotNull<T>
is implicitly cast to and fromT
so you can use it just about anywhere you need it. For example, you can pass aPerson
object to a method that takes aNotNull<Person>
:As you can see above as with nullable you would access the underlying value through the
Value
property. Alternatively, you can use an explicit or implicit cast, you can see an example with the return value below:Or you can even use it when the method just returns
T
(in this casePerson
) by doing a cast. For example, the following code would just like the code above:Combine with Extension
Combine
NotNull<T>
with an extension method and you can cover even more situations. Here is an example of what the extension method can look like:And here is an example of how it could be used:
GitHub
For your reference I made the code above available on GitHub, you can find it at:
https://github.com/luisperezphd/NotNull
Related Language Feature
C# 6.0 introduced the "null-conditional operator" that helps with this a little. With this feature, you can reference nested objects and if any one of them is
null
the whole expression returnsnull
.This reduces the number of null checks you have to do in some cases. The syntax is to put a question mark before each dot. Take the following code for example:
Imagine that
country
is an object of typeCountry
that has a property calledState
and so on. Ifcountry
,State
,County
, orCity
isnull
thenaddress will be
null. Therefore you only have to check whether
addressis
null`.It's a great feature, but it gives you less information. It doesn't make it obvious which of the 4 is null.
Built-in like Nullable?
C# has a nice shorthand for
Nullable<T>
, you can make something nullable by putting a question mark after the type like soint?
.It would be nice if C# had something like the
NotNull<T>
struct above and had a similar shorthand, maybe the exclamation point (!) so that you could write something like:public void WriteName(Person! person)
.您可以使用 C# 6 中的 Null 条件运算符以干净的方式修复 NullReferenceException,并编写更少的代码来处理 null 检查。
它用于在执行成员访问 (?.) 或索引 (?[) 操作之前测试 null。
示例
等价于:
当 p 为 null 或
p.Spouse
为 null 时,结果是 name 为 null。否则,变量名称将被赋予
p.Spouse.FirstName
的值。有关更多详细信息:空条件运算符
You can fix
NullReferenceException
in a clean way using Null-conditional Operators in C# 6 and write less code to handle null checks.It's used to test for null before performing a member access (?.) or index (?[) operation.
Example
It is equivalent to:
The result is that the name will be null when p is null or when
p.Spouse
is null.Otherwise, the variable name will be assigned the value of the
p.Spouse.FirstName
.For more details: Null-conditional Operators
有趣的是,本页上的所有答案都没有提到这两种边缘情况:
边缘情况 #1:并发访问字典
.NET 中的通用字典不是线程安全的,它们有时可能会抛出
NullReference 甚至(更常见)出现
KeyNotFoundException
。在这种情况下,异常是相当具有误导性的。边缘情况 #2:不安全代码
如果
unsafe
代码引发NullReferenceException
,您可能会查看指针变量,并检查它们是否有IntPtr.Zero
代码>或其他东西。这是同样的事情(“空指针异常”),但是在不安全的代码中,变量通常会被转换为值类型/数组等,并且你会用头撞墙,想知道值类型如何抛出这个异常例外。(顺便说一句,除非您需要,否则不使用不安全代码的另一个原因。)
边缘情况 #3:Visual Studio 多显示器设置与辅助显示器的 DPI 设置与主显示器不同
这种边缘情况是软件 -具体且适用于 Visual Studio 2019 IDE(可能还有早期版本)。
重现该问题的方法:将工具箱中的任何组件拖动到具有与主显示器不同的 DPI 设置的非主显示器上的 Windows 窗体,然后您会收到一个弹出窗口,其中显示“对象引用未设置为目的。”根据 这个线程,这个问题已经存在很长一段时间了,在撰写本文时它仍然没有得到解决。
Interestingly, none of the answers on this page mention the two edge cases:
Edge case #1: concurrent access to a Dictionary
Generic dictionaries in .NET are not thread-safe and they sometimes might throw a
NullReference
or even (more frequent) aKeyNotFoundException
when you try to access a key from two concurrent threads. The exception is quite misleading in this case.Edge case #2: unsafe code
If a
NullReferenceException
is thrown byunsafe
code, you might look at your pointer variables, and check them forIntPtr.Zero
or something. Which is the same thing ("null pointer exception"), but in unsafe code, variables are often cast to value-types/arrays, etc., and you bang your head against the wall, wondering how a value-type can throw this exception.(Another reason for non-using unsafe code unless you need it, by the way.)
Edge case #3: Visual Studio multi monitor setup with secondary monitor(s) that has different DPI setting than the primary monitor
This edge case is software-specific and pertains to the Visual Studio 2019 IDE (and possibly earlier versions).
A method to reproduce the problem: drag any component from the Toolbox to a Windows form on a non-primary monitor with different DPI setting than the primary monitor, and you get a pop-up with “Object reference not set to an instance of an object.” According to this thread, this issue has been known for quite some time and at the time of writing it still hasn't been fixed.
错误行“未将对象引用设置到对象的实例。”表明您尚未将实例对象分配给对象引用,但您仍在访问该对象的属性/方法。
例如:假设您有一个名为 myClass 的类,它包含一个属性 prop1。
现在,您正在其他类中访问此 prop1,如下所示:
上面的行会抛出错误,因为声明了类 myClass 的引用,但未实例化,或者对象的实例未分配给该类的引用。
要解决此问题,您必须实例化(将对象分配给该类的引用)。
The error line "Object reference not set to an instance of an object." states that you have not assigned an instance object to a object reference and still you are accessing properties/methods of that object.
For example: let's say you have a class called myClass and it contains one property, prop1.
Now you are accessing this prop1 in some other class just like below:
The above line throws an error because reference of class myClass is declared, but not instantiated or an instance of object is not assigned to a reference of that class.
To fix this you have to instantiate (assign an object to a reference of that class).
简单来说:
您正在尝试访问未创建或当前不在内存中的对象。
那么如何解决这个问题:
调试并让调试器中断...它会直接带您到损坏的变量...现在您的任务是简单地修复这个问题..使用new
如果它是由某些数据库命令引起的,因为对象不存在,那么您需要做的就是进行空检查并处理它:
最难的一个..如果GC已经收集了对象...如果您尝试使用字符串查找对象,通常会发生这种情况...也就是说,通过对象的名称查找它,然后 GC 可能已经清理了它...这很难找到并且将成为一个很大的问题...解决这个问题的更好方法是在开发过程中在必要时进行空检查。这将为您节省大量时间。
按名称查找是指某些框架允许您使用字符串查找对象,代码可能如下所示: FindObject("ObjectName");
Well, in simple terms:
You are trying to access an object that isn't created or currently not in memory.
So how to tackle this:
Debug and let the debugger break... It will directly take you to the variable that is broken... Now your task is to simply fix this.. Using the new keyword in the appropriate place.
If it is caused on some database commands because the object isn't present then all you need to do is do a null check and handle it:
The hardest one .. if the GC collected the object already... This generally occurs if you are trying to find an object using strings... That is, finding it by name of the object then it may happen that the GC might already cleaned it up... This is hard to find and will become quite a problem... A better way to tackle this is do null checks wherever necessary during the development process. This will save you a lot of time.
By finding by name I mean some framework allow you to FIndObjects using strings and the code might look like this: FindObject("ObjectName");
当您尝试使用的类的对象未实例化时,会发生 NullReferenceException 或未设置对象实例的对象引用。
例如:
假设您有一个名为 Student 的类。
现在,考虑另一个班级,您正在尝试检索学生的全名。
如上面的代码所示,语句
Student s - 只声明Student类型的变量,注意此时Student类还没有实例化。
因此,当执行s.GetFullName()语句时,它将抛出NullReferenceException。
NullReferenceException or Object reference not set to an instance of an object occurs when an object of the class you are trying to use is not instantiated.
For example:
Assume that you have a class named Student.
Now, consider another class where you are trying to retrieve the student's full name.
As seen in the above code, the statement
Student s - only declares the variable of type Student, note that the Student class is not instantiated at this point.
Hence, when the statement s.GetFullName() gets executed, it will throw the NullReferenceException.
从字面上看,修复 NullReferenceException 最简单的方法有两种。
例如,如果您有一个带有附加脚本的游戏对象和一个名为 rb(刚体)的变量,则当您开始游戏时,该变量将以 null 开头。
这就是您收到 NullReferenceException 的原因,因为计算机没有在该变量中存储数据。
我将使用 RigidBody 变量作为示例。
实际上,我们可以通过几种方式轻松添加数据:
然后进入脚本并输入
rb = GetComponent();
这行代码在
Start()
或Awake()
函数下效果最佳。rb = AddComponent();
进一步说明:如果您想要 Unity 要向您的对象添加组件,而您可能忘记添加组件,您可以输入
[RequireComponent (typeof(RigidBody))]
位于类声明上方(所有 using 下面的空间)。享受制作游戏的乐趣!
Literally the easiest way to fix a NullReferenceExeption has two ways.
If you have a GameObject for example with a script attached and a variable named rb (rigidbody) this variable will start with null when you start your game.
This is why you get a NullReferenceExeption because the computer does not have data stored in that variable.
I'll be using a RigidBody variable as an example.
We can add data really easily actually in a few ways:
Then go into your script and type
rb = GetComponent<Rigidbody>();
This line of code works best under your
Start()
orAwake()
functions.rb = AddComponent<RigidBody>();
Further Notes: If you want Unity to add a component to your object and you might have forgotten to add one, you can type
[RequireComponent(typeof(RigidBody))]
above your class declaration (the space below all of your usings).Enjoy and have fun making games!
这基本上是一个空引用异常。正如Microsoft所述 -
这意味着什么?
这意味着,如果任何不具有任何价值的成员,并且我们正在让该成员执行某些任务,那么系统无疑会抛出一条消息并说 -
“嘿等等,该成员没有值,所以它不能执行您正在移交的任务。”
异常本身表示正在引用某些内容,但尚未设置其值。因此,这表示它仅在使用引用类型时发生,因为值类型不可为空。
如果我们使用值类型成员,则不会发生 NullReferenceException。
上面的代码显示了分配有 null 值的简单字符串。
现在,当我尝试打印字符串 str 的长度时,我确实收到了 发生了类型为 'System.NullReferenceException' 的未处理异常 消息,因为成员 str< /strong> 指向 null 并且不能有任意长度的 null。
当我们忘记实例化引用类型时,也会发生“NullReferenceException”。
假设我有一个类和成员方法。我没有实例化我的类,只是命名了我的类。现在,如果我尝试使用该方法,编译器将抛出错误或发出警告(取决于编译器)。
上述代码的编译器会引发一个错误,即变量 obj 未分配,这意味着我们的变量具有空值或没有任何值。上述代码的编译器会引发一个错误,即变量 obj 未分配,这意味着我们的变量具有空值或没有任何值。
为什么会出现这种情况?
NullReferenceException 的出现是由于我们没有检查对象的值的错误。我们在代码开发中经常不检查对象值。
当我们忘记实例化对象时也会出现这种情况。使用可以返回或设置空值的方法、属性、集合等也可能是导致此异常的原因。
如何避免呢?
有多种方式和方法可以避免这种著名的异常:
显式检查:我们应该遵循检查对象、属性、方法、数组和集合是否为 null 的传统。这可以使用 if-else if-else 等条件语句简单地实现。
异常处理:管理此异常的重要方法之一。使用简单的 try-catch-finally 块,我们可以控制这个异常并维护它的日志。当您的应用程序处于生产阶段时,这非常有用。
空运算符:在为对象、变量、属性和字段设置值时,也可以方便地使用空合并运算符和空条件运算符。
调试器:对于开发者来说,我们身边有一个大武器——调试。如果我们在开发过程中遇到NullReferenceException,我们可以使用调试器来找到异常的来源。
内置方法:GetValueOrDefault()、IsNullOrWhiteSpace() 和 IsNullorEmpty() 等系统方法检查空值,如果存在空值,则分配默认值。
这里已经有很多好的答案。您还可以在我的 博客。
希望这也有帮助!
This is basically is a Null reference exception. As Microsoft states-
What does that mean?
That means if any member which doesn’t hold any value and we are making that member to perform certain task then the system will undoubtedly toss a message and say-
“Hey wait, that member has no values so it can’t perform the task which you are handing it over.”
The exception itself says that something is being referred but whose value is not being set. So this denotes that it only occurs while using reference types as Value types are non-nullable.
NullReferenceException won't occur if we are using Value type members.
The above code shows simple string which is assigned with a null value.
Now, when I try to print the length of the string str, I do get An unhandled exception of type ‘System.NullReferenceException’ occurred message because member str is pointing to null and there can’t be any length of null.
‘NullReferenceException’ also occurs when we forget to instantiate a reference type.
Suppose I have a class and member method in it. I have not instantiated my class but only named my class. Now if I try to use the method, the compiler will throw an error or issue a warning (depending on the compiler).
Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing. Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing.
Why does it occur?
NullReferenceException arises due to our fault for not checking the object’s value. We often leave the object values unchecked in the code development.
It also arises when we forget to instantiate our objects. Using methods, properties, collections etc. which can return or set null values can also be the cause of this exception.
How can it be avoided?
There are various ways and methods to avoid this renowned exception:
Explicit Checking: We should adhere to the tradition of checking the objects, properties, methods, arrays, and collections whether they are null. This can be simply implemented using conditional statements like if-else if-else etc.
Exception handling: One of the important ways of managing this exception. Using simple try-catch-finally blocks we can control this exception and also maintain a log of it. This can be very useful when your application is on production stage.
Null operators: Null Coalescing operator and null conditional operators can also be used in handy while setting values to objects, variables, properties and fields.
Debugger: For developers, we have the big weapon of Debugging with us. If have we face NullReferenceException during the development face we can use the debugger to get to the source of the exception.
Built-in method: System methods such as GetValueOrDefault(), IsNullOrWhiteSpace(), and IsNullorEmpty() checks for nulls and assign the default value if there is a null value.
There are many good answers already here. You can also check more detailed description with examples on my blog.
Hope this helps too!