代码改变世界

[hyddd的FindBugs分析记录][M C NP] Possible null pointer dereference

2009-02-16 11:37  hyddd  阅读(12387)  评论(4编辑  收藏  举报

[M C NP] Possible null pointer dereference [NP_NULL_ON_SOME_PATH]

There is a branch of statement that, if executed, guarantees that a null value will be dereferenced, which would generate a NullPointerException when the code is executed. Of course, the problem might be that the branch or statement is infeasible and that the null pointer exception can't ever be executed; deciding that is beyond the ability of FindBugs.

 

看一段代码:

public void test(){
  
//
  ResultSet rs = cmd.executeQuery();
  
if (rs != null && rs.next()) {
    goodsId 
= rs.getInt("goods_id");
  }
  rs.close();  //rs可能为null
  
//
}
这里不多解释,更正代码的方法很多,可以用try...catch...finally...处理可能出现的异常,也可以在rs.close之前,先判断rs是否为null。