什么是NullReferenceException,我该如何修复?
我有一些代码,当它执行时,它会引发 nullReferenceException
,说:
对象引用未设置为对象的实例。
这是什么意思,我该怎么办来解决此错误?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有一些代码,当它执行时,它会引发 nullReferenceException
,说:
对象引用未设置为对象的实例。
这是什么意思,我该怎么办来解决此错误?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(26)
tl; dr:尝试使用
html.partial
而不是renderpage
我获得了
aboce>对象参考未设置为对象的实例< /code>当我试图通过向视图呈现视图时,将其发送一个模型,例如:
调试显示该模型在Myotherview内是无效的。直到我将其更改为:
它起作用。
此外,我没有
html.partial
开始的原因是因为Visual Studio 有时 在html.partial 如果它在不同的
循环中构建不同的,即使这不是真正的错误:
但是我能够在此“错误”中没有问题而运行该应用程序。我能够通过更改
foreach
循环的结构来摆脱错误:尽管我有一种感觉是因为Visual Studio正在误读AmperSands和artarkets。
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.
continue
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中的无条件运算符以干净的方式修复
NullReferenceException
,并编写较少的代码来处理无效检查。它用于在执行成员访问(?)或索引(?[)操作之前测试NULL。
示例
等于:
结果是,当p为null或
p.p.spouse
为null时,名称将为null。否则,将分配变量名称
p.spouse.firstname
的值。对于更多详细信息: null条件运算符
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:
如果
nullReferenceException
由Unsafe
代码抛出的不安全代码,您可以查看指针变量,并检查它们是否intptr.Zero 或其他东西。这是同一件事(“ null指针异常”),但是在不安全的代码中,变量通常被铸成值类型/数组等,并且您的头撞到了墙上,想知道价值类型如何将其扔给例外。
(顺便说一句,除非需要,除非您需要不使用不安全的代码。)
边缘案例#3:带有辅助监视器的Visual Studio Multi Monitor设置,该设置的DPI设置与主监视器不同,
此EDGE CASE是Software-与“ nofollow noreferrer”> Visualio&nbsp; studio&nbsp; 2019 iDe(以及可能的versions)。
一种重现问题的方法:将任何组件从工具箱拖到具有与主监视器不同的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 。
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.
要解决此问题,您必须实例化(将对象分配给该类的引用)。
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 new 在适当位置的关键字。
如果它是在某些数据库命令上引起的,因为该对象不存在,那么您需要做的就是进行null检查并处理:
最难的一个..如果 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或对象引用未设置为对象的实例。
For example:
Assume that you have a class named Student.
现在,考虑一下您试图检索学生全名的另一堂课。
如以上代码所示,该语句
学生S - 仅声明学生类型的变量,请注意,此时尚未实例化学生班。
因此,当语句 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.
从字面上看,修复nullReferenceExeption的最简单方法有两种方式。
如果您有一个gameObject,例如带有脚本的脚本和一个名为 rb (刚体)的变量,则该变量将在您启动游戏时以null开头。
这就是为什么您获得NullReferenceExeption的原因,因为计算机没有存储在该变量中的数据。
我将以一个刚性变量为例。
实际上,我们可以通过几种方式可以很容易地添加数据:
然后进入您的脚本并键入
rb = getComponent&lt; strigbody&gt;();
这一行代码在您的
start()
或awake()
函数下工作。rb = addComponent&lt; strig&gt;();
进一步注释:如果您想要 unity 要在对象中添加一个组件,您可能已经忘记了一个,您可以键入一个
[sirceseComponent (typeof(arigidbody))]
在您的类声明上方(使用 s的所有下方的空间)。享受并玩得开心!
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!
这基本上是一个零参考异常。 AS microsoft> Microsoft
这意味着什么?
这意味着,如果任何不具有任何价值的成员,我们正在使该成员执行某些任务,那么该系统无疑会扔出消息,然后说 -
“嘿,等等,该成员没有值,因此无法 的任务。
执行您要交出 因此,这表示仅在使用参考类型作为值类型时才发生。
如果我们使用Value类型成员,则不会发生NullReferenceException。
上面的代码显示了用 null 值分配的简单字符串。
现在,当我尝试打印字符串的长度 str 时,我确实会出现 'system.nullReferenceException'的类型消息,因为成员 str 消息/strong>指向零,没有任何时间的空。
' nullReferenceException '当我们忘记实例化参考类型时,也会发生。
假设我有一个类和成员方法。我没有实例化我的班级,但只命名了我的班级。现在,如果我尝试使用该方法,编译器将发出错误或发出警告(取决于编译器)。
上述代码的编译器提出了一个错误,即变量 obj 未分配,这表明我们的变量具有零值或没有。上述代码的编译器提出了一个错误,即变量 obj 未分配,这表明我们的变量具有零值或没有。
为什么发生?
nullReferenceException由于我们没有检查对象的值而出现了。我们经常将对象值放在代码开发中未选中。
当我们忘记实例化对象时,它也会出现。使用方法,属性,集合等可以返回或设置为空值的方法也可能是此例外的原因。
如何避免它?
有多种避免此著名例外的方法和方法:
明确检查:我们应该遵守检查对象,属性,方法,阵列和集合的传统,无论它们是否为无效。可以简单地使用条件语句(例如if-else if-else等)实现。
。
异常处理:管理此例外的重要方法之一)来实现这一点。使用简单的尝试键入块,我们可以控制此异常并保持其日志。当您的应用程序处于生产阶段时,这可能非常有用。
null运算符:无效的合并操作员和无条件操作员也可以用方便地使用,同时将值设置为对象,变量,属性和字段。
调试器:对于开发人员来说,我们拥有与我们一起调试的重要武器。如果我们在开发面前面对NullReferenceException,我们可以使用调试器来达到例外的来源。
内置方法:诸如getValueordEfault(),isnullorWhitespace()和isnullorement()检查nulls的系统方法,如果有空值。
,请分配默认值。
这里已经有很多好的答案。您还可以在我的博客。
希望这也有帮助!
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!
continue
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异常 - 视觉基本
nullReference异常
Visual Basic 与 c#中的一个没有什么不同。毕竟,他们都报告了他们俩使用的.NET框架中定义的相同例外。视觉基础所独有的原因是罕见的(也许只有一个)。该答案将使用视觉基本术语,语法和上下文。使用的示例来自大量过去的堆栈&nbsp;溢出问题。这是通过使用帖子中经常看到的情况的种类来最大化相关性。还为可能需要它的人提供了更多解释。与您类似的示例是非常可能在此处列出的示例。
注意:
nullReferenceException
(NRE),如何找到它,如何修复它以及如何避免它的原因。 NRE可以引起多种方式,因此这不太可能成为您的唯一遇到。基本含义
消息“对象未设置为对象实例” 表示您正在尝试使用尚未初始化的对象。这归结为其中之一:
找到原因
,因为问题是
note
nothere 检查他们以找出哪一个。然后确定为什么不初始化。将鼠标固定在各种变量上,Visual Studio(VS)将显示其值 - 罪魁祸首将为Nothing
。您还应该从相关代码中删除任何尝试/捕获块堵塞。这将导致您的代码尝试使用
nothing
的对象时崩溃。 这就是您想要的,因为它将标识问题的精确位置,并允许您识别引起它的对象。msgbox
在捕获量中显示错误,而...
几乎没有帮助。此方法还导致非常糟糕的 stack&nbsp;溢出问题,因为您无法描述实际例外,涉及的对象甚至代码行的发生在哪里。您还可以使用
当地窗口
( debug - &gt; windows - &gt;当地人)检查您的对象。一旦您知道问题所在,通常比发布一个新问题相当容易修复和更快。
另请参阅:
例外
http://msdn.microsoft.com/en-us/library/seyhszts(v=vs.110).aspx“ rel =“ noreferrer”> msdn : 一个实例
是
dim
不创建CashRegister 对象;它仅声明该类型的名为reg
的变量。 声明一个对象变量和创建实例是两种不同的事情。补救件
新
通常可以使用运算符来创建实例时:当仅适用于以后创建实例时:
注意: do not < /strong>使用
dim
在一个过程中再次,包括构造函数(sub new
):这将创建 local variable,
reg
,仅在此上下文(sub)中存在。reg
带模块级别的变量范围
您将在其他任何地方使用nothing
。需要清楚,
dim
(或private
)仅声明一个变量及其type
。该变量的范围 - 是为整个模块/类存在还是在过程中本地化的范围 - 由确定 被声明。私人|朋友| public
定义访问级别,而不是范围。有关更多信息,请参见:
数组
数组也必须进行实例化:
仅声明,而不是创建此数组。有几种初始化数组的方法:
注意:从VS 2010开始,当使用文字和
选项初始化本地数组时,
,as&lt; type; type&gt;
and <代码>新元素是可选的:从分配的数据中推断出数据类型和数组大小。类/模块级别的声明仍然需要
as type&gt;
option strict
:示例:类对象的数组
已经创建了数组,但是
foo
对象没有。补充
使用
列表(t)
将使没有有效对象的元素很难:有关更多信息,请参见:
列表和集合
。还必须实例化或创建净收集(其中有许多品种 - 列表,字典等)。
出于相同的原因,您会获得相同的例外 -
myList
仅声明,但没有创建实例。补救措施是相同的:一个常见的监督是使用Collection
type
的类:任何一个过程都会导致NRE,因为
barlist
仅声明,而不是实例化。创建foo
的实例也不会创建内部barlist
的实例。可能是在构造函数中执行此操作的目的:和以前一样,这是不正确的:
有关更多信息,请参见
list(t)
class 。与数据库一起使用的数据提供商对象
为nullReference提供了许多机会,因为可能有许多对象(
命令
,Connection
,trans> trassaction
,dataSet
,dataTable
,datarows
....)一次使用。 注意:您正在使用哪个数据提供商 - MySQL,SQL Server,OLEDB等 - Concepts 是相同的。示例1
和以前一样,声明了
ds
数据集对象,但是从未创建实例。dataAdapter
将填充现有的数据集
,而不是创建一个。在这种情况下,由于ds
是局部变量,因此IDE警告您可能会发生:。不要忽略警告。
补救症
示例2
在这里错字是一个问题:
员工
vs雇员
。没有DataTable
命名为“员工”创建的,因此nullReferenceException
尝试访问它的结果。另一个潜在的问题是假设将有项目
,当SQL包含一个where子句时,可能并非如此。补救症
由于使用一个表,使用
表(0)
将避免拼写错误。检查行。COUNT.COUNT
也可以帮助:fill
是返回行的数量
受影响的功能:示例3
dataAdapter
将提供tableNames
,如上一个示例所示,但不会从SQL或数据库表中解析名称。结果,ds.table(“ ticket_reservation”)
引用不存在的表。补救症是相同的,请参考索引:
另请参见 DataTable类。
对象路径/嵌套
代码仅测试
项目
,而myfoo
和bar
也可能没有。 补救症是一次测试一个对象的整个链或路径:andoLSO
很重要。一旦遇到第一个false
条件,将不会执行后续测试。这允许代码一次安全地“钻入”一个“级别”,仅在(如果)myfoo
之后评估myfoo.bar
才能确定有效。编码复杂对象时,对象链或路径可能会变得很长:不可能引用
null
对象的任何“下游”的内容。这也适用于控件:此处,
mywebbrowser
或document
可能不存在formfld1
元素可能不存在。UI控件
除其他方面,此代码不会预期用户可能不会在一个或多个UI控件中选择某些内容。
ListBox1.SelectedItem
很可能是nothing
,因此listbox1.selectedItem.toString
将导致NRE。Resedy
在使用该数据之前验证数据(还使用
strict
和SQL参数):或者,您可以使用
(combobox5.SelectedItem issonnot nothot nothot) /code>
Visual Basic表单
这是获得NRE的相当常见方法。在C#中,根据其编码方式,IDE将报告
Controls
在当前上下文中不存在,或“不能引用非静态成员”。因此,在某种程度上,这是仅VB的情况。它也很复杂,因为它会导致故障级联。无法以这种方式初始化阵列和集合。此初始化代码将在之前运行 构造函数创建
form> form
或Controls 。结果:
somevar
分配的五个元素,因为没有什么都没有.Text 属性
引用数组元素以后会导致NRE。如果您在
form_load
中执行此操作,则由于奇数错误,IDE 可能不会在发生时报告异常。当您的代码试图使用数组时,此例外将弹出以后。这个“沉默异常”是在这篇文章中详细介绍。出于我们的目的,关键是,当创建表单时发生灾难性的事情(sub new
或表单加载
事件)时,异常可能会毫无报告为只是显示表单。由于您的
sub new
或form load
事件将在NRE之后运行,因此 可以使很多其他事情都可以不进行原始化。这适用于任何和所有控制和组件的参考文献,使这些参考是非法的
注释
。 声明在形式级别上的容器,但是 当控件 do 存在时,在形式负载事件处理程序中它们。只要您的代码在
oniratizecomponent
调用:数组代码可能还不属于树林之后,就可以在
都将再次存储在这些数组元素中,并且在您尝试引用它时会产生NRE。
sub new
中完成。在容器控件中的任何控件(例如groupbox
或面板
)都不会在me.controls
中找到;他们将在该面板或GroupBox的控件集合中。拼写控件名称时,也不会返回控件(“ TestBox2”
)。在这种情况下,现在您知道您要寻找的东西,这些应该很容易找到:

“ button2”位于
panel
补救措施 上
而不是使用表单的
Controls
集合而不是通过名称进行间接
参考值和
nullReferenceException
可能结果'。您可以通过退出功能
返回来抑制警告,但这并不能解决问题。任何试图使用返回时somecondition = false
的任何试图使用
返回Blist 。返回空
list
与返回nothing
不同。如果返回的对象可以没有
,请在使用之前进行测试:实现/捕获不良实现的尝试
/捕获可能会隐藏问题的位置,并导致新的问题:
这是未预期创建对象的情况,但也演示了空的
catch
的对抗有用性。SQL中有一个额外的逗号(在“ Mailaddress”之后),在
.executereader
中导致例外。catch
什么都不做之后,最后试图进行清洁,但是由于您不能close> close
nulldatareader
object ,一个全新的nullReferenceException
结果。空的
catch
块是魔鬼的游乐场。这种操作让他感到困惑,为什么他在终于
块中获得NRE。在其他情况下,一个空的catch
可能会导致其他事情进一步出现在下游,并使您花时间在错误的位置查看错误的问题。 (上述上述的“沉默异常”提供了相同的娱乐价值。)Remedy
不要使用空的尝试/捕获块 - 让代码崩溃,以便您可以a)确定原因b)确定位置c)应用适当的补救措施。尝试/捕获块并不是要隐藏异常的人,以解决这些人的唯一资格 - 开发人员。
dbnull与
isdbnull
函数不一样,用于测试如果a value equals
system.dbnull
:的rel =“ noreferrer”>Remedy
与以前一样,您可以一无所获,然后对于特定值:
示例2
firstordEfault
返回第一个项目或默认值,该值是参考类型的无
,从不 dbnull :控制
如果
复选框
带有chkname
找不到(或存在于groupbox
)中,然后chk
将一无所有,并试图引用任何属性会导致例外。补救
datagridView
dgv会定期看到一些怪癖:
如果
dgvbooks
具有autogeneratecolumns = true
,它将创建列,但不会命名它们,因此上述代码以名称引用时会失败。Remedy
手动命名列,或通过索引参考:
请当心newRow。
示例2 -当您的
dataGridView
具有allowusertoAddrows
attrue
true <时, /code>(默认值),底部的空白/新行中的
单元格
都包含nothere
。大多数使用内容的尝试(例如,toString
)将导致NRE。补救症
使用
for/enver
循环并测试isNewrow
属性属性来确定是否是最后一行。如果allow usertoAddrows
是真的:如果您确实使用循环,请修改行计数或使用 exit 时> isNewrow 是正确的。
在某些情况下,尝试使用
my.settings
的My.Settings(StringCollection),它是
StringCollection
可能在您第一次使用时会导致nullReference。解决方案是相同的,但不那么明显。考虑:由于VB正在为您管理设置,因此可以期望它初始化该集合是合理的。它会,但是只有以前您以前已经在集合中添加了初始条目(在“设置编辑器”中)。由于(显然)在添加项目时(显然是)初始化了该集合,因此当设置编辑器中没有项目要添加时,它仍然是
nothing
。Remedy
初始化表单
load> Load
事件处理程序中的设置集合,如果/在需要时:通常,
settings
Collection只需要初始化第一次运行应用程序。另一种补救措施是在 project -&gt中为您的收藏中添加初始价值;设置| Foobars ,保存项目,然后删除假值。关键点
您可能忘记了
新
操作员。或者
您认为可以完美地执行以将初始化的对象返回您的代码。
不要忽略编译器警告(永远),并且使用
选项严格在
上(始终)。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对象施加到a 。例如,下面的代码:
它将在铸件上抛出
nullReferenceException
。在上面的示例中似乎很明显,但是这可以在更多的“后期”错综复杂的方案中发生,在这些方案中,从您不拥有的某些代码返回了空对象,并且演员表是由某些自动系统生成的。一个示例是使用日历控件的简单ASP.NET绑定片段:
selected -Date
实际上是dateTime
type -ofcalendar的属性 -
Web控制类型,绑定可以完美返回一些空物。隐式ASP.NET生成器将创建一个代码,该代码等效于上面的铸造代码。这将引起很难发现的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的对象引用变量(即它没有引用实际对象实例)。
为了防止错误,在使用之前应测试可为空测试的对象。
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'“ noreferrer”> 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。
与一个参数努力相比,如果某种方法期望将其传递给它不是零,则通常将其作为防御措施进行。
更多信息在 c#nullreferenceException和null parameter 。
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引入无效的参考类型和不可删除的参考类型。因此,仅必须检查可无效的参考类型,以避免 nullReferenceException 。
如果您尚未初始化参考类型,并且要设置或读取其属性之一,它将抛出 nullReferenceException 。
示例:
您可以通过检查变量是否没有零来避免这种情况:
要完全理解为什么要抛出NullReferenceException,重要的是要了解值类型和[参考类型] [3]。
因此,如果您要处理价值类型,则NullReferenceExceptions可能不会发生 。尽管在处理参考类型时,您需要保持警惕!
只有名称建议的参考类型才能将参考文献或点字面上的内容(或“ null”)保存。而价值类型始终包含一个值。
参考类型(必须检查这些类型):
值类型(您可以简单地忽略这些类型):
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
andcar
是不兼容的类型;CAR
无法转换/铸造到book
。当此演员失败时,返回null
。使用mybook
在导致nullReferenceException
之后。通常,您应该使用cast或
作为
,如下:如果您期望类型转换始终成功(即。演员:
如果您不确定类型,但是要尝试将其用作特定类型,然后将
用作
: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值参考的对象。因此,它给出了无效的例外。在示例中,字符串值为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 以及避免/修复的方法在其他答案中已经解决了这样一个例外,许多程序员还没有学到的是如何独立debug debug 这样开发过程中的例外。
在Visual Studio中,由于“ noreferrer”> Visual Studio Debugger 。
首先,确保将要捕获正确的错误 - 查看
我如何允许在VS2010中打破'System.NullReferenceException'? /em>
然后从调试(F5) 或 将[VS调试器]附加到运行过程 。有时使用
debugger.break
,它将提示启动调试器。现在,当将nullReferenceException抛弃(或未经处理)时,调试器将停止(记住上面的规则集?)。有时,错误很容易发现。
例如,
在以下行中, can 的唯一代码是例外是
mystring
评估为null。可以通过查看观看窗口或在在更高级的情况下,例如以下情况,您需要使用上面的技术之一(观看或立即的窗口)来检查表达式以确定
str1
是否为null或str2是否为null
是无效的。一旦, throw的位置已找到,通常会很琐碎地向后推理以找出零值的位置[错误地] -
请花时间了解例外的原因所需的时间。检查无效表达式。检查以前可能导致此类零表达式的表达式。添加并遵守程序。 使用调试器。
1 如果打开掷出过于侵略性,并且调试器在.net或第三方库中的NPE上停止,则可以使用用户 - 毫无用处 break 可用于限制捕获的异常。此外,VS2012引入只是我的代码我也建议启用。
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给了这个示例:
其中 unboxing 转换(铸造(铸造) )来自
object
(或从类别之一system.valuetype
或system.enum
,或从接口type) to 一个值类型(nullable&lt;&gt;
)本身给出了nullReferenceException
。在另一个方向上,来自 a a
nullable&lt;&gt;&gt;
的 拳击 转换代码>等于false
to 参考类型,可以给出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,其CodeBehind类是联系人,并且您具有实体名称联系人。
请在调用Context.SaveChanges()时,以下代码将丢弃NullReferenceException
然后,为了完整的dataContext类
和联系人实体类, 。有时,实体类是部分类,因此您也可以在其他文件中扩展它们。
当实体和CodeBehind类处于同一名称空间时,发生错误。
要解决此问题,请重命名实体类或CodeBehind类以供contact.aspx。
原因
我仍然不确定原因。但是,每当任何实体类都扩展system.web.ui.page时,此错误就会发生。
为了讨论,请查看 nullReReference exception in dbcontext.savechanges()
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
的所有属性。请参阅“ nullReReferenizAttribute> nullReReferenizectexception在测试自定义授权attribute 时,就会丢弃。
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.
我有不同的看法来回答这个问题。这种答案“我还能做些什么来避免它
? 。在这种情况下,依赖注入容器可以用来初始化服务以避免 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.
关于“我该怎么办” ,可以有很多答案。
在开发时,一种更“正式的”方法是 通过合同设计 在您的代码中。这意味着您需要在开发过程中在系统上设置类不变性和/或偶数函数/方法先前的前提和后条件。
简而言之)。 先决条件是指作为函数/方法输入的数据必须遵循某些约束,并且 postciptions 表示函数/方法输出必须再次遵循集合约束,而不会违反它们。
合同条件应在执行免错误程序期间永远不会违反,因此在调试模式下通过合同进行设计,而在发行版中被禁用,以最大程度地提高开发的系统性能。
这样,您可以避免
nullReferenceException
是违反设置约束的结果的情况。例如,如果您在类中使用对象属性x
,然后尝试调用其一种方法之一,x
具有空值,则将导致nullReferenceException
:但是,如果将“属性X绝不具有零值”为方法前提,则可以防止以前描述的方案:
为此, 代码合同 project for .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.
当我们试图访问空对象的属性或字符串值变为空并且我们正在尝试访问字符串方法时,
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: