简体   繁体   中英

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.

then, is there any way to find whether a is null of b is 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() . If it crashes your problem is with the b object. If not Step Out from b.do() and Step Into a.do() and find your problem there.

I must say though Tomasz's approach is much safer and recommended to use as it is also readable.

Either a or b is null for that statement to cause an error. You cannot see which, except by checking each object seperately.

Also, isn't "do" a reserved keyword? Not sure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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