探测目标对象是什么时,引发NullReferenceException [英] Detecting what the target object is when NullReferenceException is thrown

查看:122
本文介绍了探测目标对象是什么时,引发NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,我们都收到了异常,在一段时间或其他奇妙模糊的未设置为一个对象实例的参考。确定这是问题的对象往往是设置断点和检查所有成员在每个语句的繁琐的任务。

I'm sure we all have received the wonderfully vague "Object reference not set to instance of an Object" exception at some time or another. Identifying the object that is the problem is often a tedious task of setting breakpoints and inspecting all members in each statement.

没有人有任何技巧,轻松,高效地确定导致异常的对象,无论是通过的程序化或其他方式?

Does anyone have any tricks to easily and efficiently identify the object that causes the exception, either via programmatical means or otherwise?

- 修改

看来我是模糊的,如异常=)。问题的关键是_not必须调试应用程序,找到错误的对象。编译器/运行时不知道该对象已被分配的/声明,并且该对象还未被实例化。有没有一种方法来提取/识别一个捕获的异常那些细节

It seems I was vague like the exception =). The point is to _not have to debug the app to find the errant object. The compiler/runtime does know that the object has been allocated/declared, and that the object has not yet been instantiated. Is there a way to extract / identify those details in a caught exception

@ W·克雷格交易

您的解释,这是一个设计问题的结果很可能是我能得到最好的答案。我相当强迫防御性编码,并已成功地摆脱大多数误差随时间固定我的习惯后。其余的人只是 的调整我没有尽头,而导致我发布这个问题给社会。

Your explanation that it is a result of a design problem is probably the best answer I could get. I am fairly compulsive with defensive coding and have managed to get rid of most of these errors after fixing my habits over time. The remaining ones just tweak me to no end, and lead me to posting this question to the community.

感谢大家的建议。

推荐答案

目前所在的NRE被抛出了点,没有目标对象 - 这是异常点。你可以期待的最多的就是陷阱发生异常的文件和行号。如果您在发现问题哪个对象引用导致了问题,那么你可能要重新考虑你的编码标准,因为它听起来像你做太多一行code。

At the point where the NRE is thrown, there is no target object -- that's the point of the exception. The most you can hope for is to trap the file and line number where the exception occurred. If you're having problems identifying which object reference is causing the problem, then you might want to rethink your coding standards, because it sounds like you're doing too much on one line of code.

要这样的问题更好的解决方案是契约式设计,无论是通过内置的语言结构,或通过库。 DbC的建议pre-检查的方法超出范围的数据的传入的参数(即:无),并抛出异常,因为该方法不会有坏数据的工作

A better solution to this sort of problem is Design by Contract, either through builtin language constructs, or via a library. DbC would suggest pre-checking any incoming arguments for a method for out-of-range data (ie: Null) and throwing exceptions because the method won't work with bad data.

我觉得NRE描述误导你。在CLR被遇到的问题是,它被要求解除引用的对象引用,当对象基准为Null。就拿这个例子程序:

I think the NRE description is misleading you. The problem that the CLR is having is that it was asked to dereference an object reference, when the object reference is Null. Take this example program:

public class NullPointerExample {
  public static void Main()
  {
    Object foo;
    System.Console.WriteLine( foo.ToString() );
  }
}

当你运行它,它会引发NRE第5行,当它试图评估于foo的ToString()方法。有没有对象,调试,只有未初始化的对象引用(富)。有一类和方法,但没有对象。

When you run this, it's going to throw an NRE on line 5, when it tried to evaluate the ToString() method on foo. There are no objects to debug, only an uninitialized object reference (foo). There's a class and a method, but no object.

回复:克里斯Marasti - 乔格的<一个href="http://stackoverflow.com/questions/115573/detecting-what-the-target-object-is-when-nullreferenceexception-is-thrown#115619">answer:

Re: Chris Marasti-Georg's answer:

您永远不应该自己扔NRE - 这是具有特定含义的系统异常:在CLR(或JVM)试图评估没有初始化的对象引用。如果pre-检查的对象引用,然后要么抛出某种无效参数异常或应用程序特定的异常,但不是NRE的,因为你只会混淆谁也维护你的应用程序中的下一个程序员。

You should never throw NRE yourself -- that's a system exception with a specific meaning: the CLR (or JVM) has attempted to evaluate an object reference that wasn't initialized. If you pre-check an object reference, then either throw some sort of invalid argument exception or an application-specific exception, but not NRE, because you'll only confuse the next programmer who has to maintain your app.

这篇关于探测目标对象是什么时,引发NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆