Gracefully avoiding NullPointerException in Java

前端 未结 9 1403
夕颜
夕颜 2020-11-30 04:02

Consider this line:

if (object.getAttribute(\"someAttr\").equals(\"true\")) { // ....

Obviously this line is a potential bug, the attribute

9条回答
  •  被撕碎了的回忆
    2020-11-30 04:45

    I like option 1 and I would argue that it is readable enough.

    Option 3 btw would be to introduce a getAttribute method that takes a default value as a parameter.

提交回复
热议问题