“NullReferenceException”的可能原因除了取消引用设置为 null 的对象引用之外
我知道当 NullReferenceException
被抛出,错误消息指出:
对象引用未设置为 对象的实例。
我意识到当我尝试取消引用 null
对象引用时会抛出此消息。
该错误消息暗示,除了对象引用为 null
(可能是错误的内存地址或类似内容)之外,可能还有其他原因导致 NullReferenceException
。是这样吗?
编辑:我更关心可能抛出NullReferenceException
的原因,而不是错误消息的措辞。错误消息的措辞正是引发问题的原因。
I know that when a NullReferenceException
gets thrown, the error message states:
Object reference not set to an
instance of an object.
And I realize this message is thrown when I attempt to dereference a null
object reference.
The error message implies that there could be a reason for a NullReferenceException
besides an object reference being null
(perhaps a bad memory address or something similar). Is this the case?
Edit: I'm more concerned with the reasons aNullReferenceException
could be thrown than the wording of the error message. The wording of the error message is just what prompted the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我怀疑它试图保持语言中立。例如,Visual Basic 程序员习惯于“无”而不是“空”。显然,类型名称不是语言中立的,但至少如果消息是语言中立的,那就是一个开始。
我不知道如果您试图以某种方式取消引用“坏”内存地址,您会得到这个信息...此时会涉及到更严重的 CLR 错误。
另一种可能性是,它试图避免声明您已将引用的值显式设置为 null - 例如,它可能只是引用类型变量的默认值。
I suspect it's trying to be language-neutral. A Visual Basic programmer is used to "nothing" instead of "null", for example. Obviously the type name isn't language neutral, but at least if the message is, that's a start.
I don't know that you'd get this if somehow you tried to dereference a "bad" memory address... at that point there's a far worse CLR error involved.
Another possibility is that it's trying to avoid stating that you've explicitly set the value of the reference to null - it could be just the default value of a reference type variable, for example.
说引用设置为 null 并没有说明需要做什么。为了变得更加“有帮助”,他们告诉您需要发生什么:需要将对象引用设置为对象的实例。
我并没有真正将其理解为暗示除了引用为空之外可能还有其他原因。如果未初始化,则会导致编译错误。最重要的是,该引用在您尝试使用它时并未引用任何内容。
Saying the reference is set to null doesn't say a whole lot about what needs to be done. In their attempt to be more "helpful", they are telling you what needs to happen: The object reference needs to be set to an instance of an object.
I don't really read that as implying there could be other reasons besides a reference being null. If it was left uninitialized, it causes a compile error. The bottom line is that the reference doesn't refer to anything at the time you attempted to use it.
当一个对象像这样声明时:
它被定义为对象引用。但是,由于它为 null(并且尚未实例化),因此未将其设置为对象的实例。如果我们添加:
则引用已设置为该对象的新实例。
如果我们稍后将其设置为 null:
那么它又处于 null 状态。因此,异常消息涵盖了两种情况(未实例化或显式设置为 null)。
When an object is declared like so:
it is defined as an object reference. However, because it is null (and has not been instantiated), it is not set to an instance of an object. If we add:
then the reference has been set to a new instance of the object.
If we later set it to null:
Then it is again in the null state. Therefore, the exception message covers both cases (not instantiated or explicitly set to null).
它是公共语言规范的一部分。看一下静态方法和实例方法之间的区别。
静态方法属于类型,因此不需要实例。如果您查看 IL 字节代码,就会发现静态方法被称为“call”。
但是,根据定义,非静态方法是实例方法并且需要实例。 IL 指令是“callvirt”。 “call”和“callvirt”之间的主要区别在于“callvirt”检查以查看调用方法的目标不为空。
It's part of the Common Language Specification. Take a look at the difference between a static method and an instance method.
A static method belongs to the Type, and as such does not require an instance. If you look at the IL byte code, static methods are referred to as "call".
However, a non-static method is by definition an instance method and requires an instance. The IL instruction is "callvirt". The key difference between "call" and "callvirt" is that "callvirt" checks to see that the target to invoke the method against is not null.