如何在一个getter链中跟踪一个NullPointerException [英] How to trace a NullPointerException in a chain of getters

查看:165
本文介绍了如何在一个getter链中跟踪一个NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在这样的调用中得到NullPointerException:

  someObject.getSomething()。getSomethingElse()。 
getAnotherThing()。getYetAnotherObject()。getValue();

我得到一个非常无用的异常文本,如:



线程main中的异常java.lang.NullPointerException中的
在package.SomeClass.someMethod(SomeClass.java:12)

我觉得很难发现调用实际上是returend null,经常发现自己将代码重构为这样的东西:

  Foo ret1 = someObject.getSomething(); 
Bar ret2 = ret1.getSomethingElse();
Baz ret3 = ret2.getAnotherThing();
Bam ret4 = ret3.getYetAnotherOject();
int ret5 = ret4.getValue();

然后等待一个更具描述性的NullPointerException,告诉我要查找哪一行。你们中有些人可能会认为连接getter是坏的风格,应该避免,但我的问题是:我可以在不改变代码的情况下找到错误吗?提示:我正在使用eclipse,我知道调试器是什么,但是我无法理解如何将其应用于该问题。



我对答案的结论:

有些答案告诉我,我不应该连续吸烟,一些答案显示我如何调试我的代码,如果我不服从这个建议。



我已经接受了一个答案,教我如何链接getter:




  • 如果他们不能返回null,只要你喜欢链接它们。不需要检查!= null,不需要担心NullPointerExceptions(被警告,链接仍然注明了demeter的规则,但我可以生活在那个

  • 如果他们可能返回null,永远不会链接它们,并对每个可能返回null的null值进行检查。



这对于实际的调试没有任何好的建议。

解决方案

答案取决于你的看法)你的吸气如果他们可能返回 null ,那么你应该每次都检查一下返回值。如果getter不应该返回 null ,那么getter应该包含一个检查并抛出异常( IllegalStateException ?)而不是返回 null ,您答应永远不会返回。堆栈跟踪将指向您确切的吸气剂。您甚至可以将异常消息中发现的异常状态置于异常消息中。


If I get a NullPointerException in a call like this:

someObject.getSomething().getSomethingElse().
    getAnotherThing().getYetAnotherObject().getValue();

I get a rather useless exception text like:

Exception in thread "main" java.lang.NullPointerException
at package.SomeClass.someMethod(SomeClass.java:12)

I find it rather hard to find out wich call actually returend null, often finding myself refactoring the code to something like this:

Foo ret1 = someObject.getSomething();
Bar ret2 = ret1.getSomethingElse();
Baz ret3 = ret2.getAnotherThing();
Bam ret4 = ret3.getYetAnotherOject();
int ret5 = ret4.getValue();

and then waiting for a more descriptive NullPointerException that tells me which line to look for.

Some of you might argue that concatening getters is bad style and should be avoided anyway, but my Question is: Can I find the bug without changing the code?

Hint: I'm using eclipse and I know what a debugger is, but I can't figuere out how to apply it to the problem.

My conclusion on the answers:
Some answers told me that I should not chain getters one after another, some answers showed my how to debug my code if I disobayed that advice.

I've accepted an answer that taught me excactly when to chain getters:

  • If they cannot return null, chain them as long as you like. No need for checking != null, no need to worry about NullPointerExceptions (be warned that chaining still vialotes the law of demeter, but I can live with that)
  • If they may return null, don't ever, never ever chain them, and perform a check for null values on each one that may return null

This makes any good advice on actual debugging useless.

解决方案

The answer depends on how you view (the contract of) your getters. If they may return null you should really check the return value each time. If the getter should not return null, the getter should contain a check and throw an exception (IllegalStateException?) instead of returning null, that you promised never to return. The stacktrace will point you to the exact getter. You could even put the unexpected state your getter found in the exception message.

这篇关于如何在一个getter链中跟踪一个NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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