Gracefully avoiding NullPointerException in Java

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

    I have another answer;

    List> group = jjDatabase.separateRow(db.Select("SELECT * FROM access_user_group  WHERE user_id=1 ;"));
    

    there is not "group_c80" as column in 'access_user_group' in my database, so in get(0).get("group_c80") null pointer exception accords. But i handled it through below code :

    for (int j = 1; j < 100; j++) {
                        String rulId="0";//defult value,to privent null pointer exeption in group_c
                        try {
                            rulId = group.get(0).get("group_c" + j)).toString();
                        } catch (Exception ex) {
                            ServerLog.Print( "Handeled error in database for " + "group_c" + (j < 10 ? "0" + j : j) +"This error handeled and mot efect in program");
                            rulId = "0";
                        }}
    

提交回复
热议问题