简体   繁体   English

我可以找到NullPointerException的来源吗?

[英]Can I find where a NullPointerException came from?

When I'm trying the following statement, 当我尝试以下语句时,

a.do(b.do());

suppose I got a NullPointerException from that line. 假设我从那一行得到了NullPointerException。

then, is there any way to find whether a is null of b is null? 那么,有什么方法可以找到a是否为null,b是否为null?

No, the only solution is to extract the inner expression: 不,唯一的解决方案是提取内部表达式:

Object o = b.do()
a.do(o);

Which is a good idea anyway. 无论如何,这是一个好主意。

If you debug your program in eclispe or any standard IDE you can do Step Into which will first execute b.do() . 如果您在eclispe或任何标准IDE中调试程序,则可以执行Step Into ,它将首先执行b.do() If it crashes your problem is with the b object. 如果崩溃,则问题出在b对象上。 If not Step Out from b.do() and Step Into a.do() and find your problem there. 如果不是,请从b.do() Step OutStep Into a.do()然后在其中找到问题。

I must say though Tomasz's approach is much safer and recommended to use as it is also readable. 我必须说,尽管Tomasz的方法更安全,并建议使用,因为它也易于阅读。

Either a or b is null for that statement to cause an error. 对于该语句,a或b为null会导致错误。 You cannot see which, except by checking each object seperately. 除了单独检查每个对象,您看不到哪个。

Also, isn't "do" a reserved keyword? 另外,不是“保留”关键字吗? Not sure. 不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM