Find out what variable is throwing a NullPointerException programmatically

后端 未结 8 1602
醉酒成梦
醉酒成梦 2020-12-05 10:30

I know I can find out if a variable is null in Java using these techniques:

  • if (var==null) -> too much work
  • try { ... } catch (Null
相关标签:
8条回答
  • 2020-12-05 10:56

    I think you should notice Demeters Law.

    There aren't many people following it strictly, because it results in a lot of delegate methods.
    But getting too far from it will result in dependencies on inner structures that should be transparent.

    0 讨论(0)
  • 2020-12-05 10:57

    Unfortunately, Java will not show you the name of the variable or the exact position of an error other than the line number. If you use Eclipse you can use nullable annotations however, see http://www.fosslc.org/drupal/content/bye-bye-npe for example. See Which @NotNull Java annotation should I use? for other annotation systems.

    0 讨论(0)
提交回复
热议问题