Gracefully avoiding NullPointerException in Java

前端 未结 9 1330
夕颜
夕颜 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:38

    It's a very good question. I usually use the not graceful:

    if (object.getAttribute("someAttr") != null && object.getAttribute("someAttr").equals("true")) { // ....
    

    (and I will not use it anymore)

提交回复
热议问题