且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

对象引用不设置到对象的实例 - 解释?

更新时间:2021-07-21 07:58:16

要解释它的最简单的方法是,如果你的对象引用(你的变量)是,然后你不能没有触发该异常访问它的任何属性或方法。下面是code为例,将抛出一个空参考异常(因为他们是所谓):

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

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

所以我定义了一个字符串,但随后尝试访问其长属性。发生的异常。如果我用的一个方法 字符串的ToString(),除了会发生为好。

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

通过跟踪沿着这条错误的问题是,你可以从错误文本看,你看不出来马上对空参考的地方出现。你必须在一个断点异常发生前将走在code,直到你发现有问题的作品。

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