[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(){
//![[hyddd的FindBugs分析记录][M C NP] Possible null pointer dereference [hyddd的FindBugs分析记录][M C NP] Possible null pointer dereference](/default/index/img?u=L2RlZmF1bHQvaW5kZXgvaW1nP3U9TDJsdFlXZGxjeTlrYjNRdVoybG0=)
ResultSet rs = cmd.executeQuery();
if (rs != null && rs.next()) {
goodsId = rs.getInt("goods_id");
}
rs.close(); //rs可能为null
//![[hyddd的FindBugs分析记录][M C NP] Possible null pointer dereference [hyddd的FindBugs分析记录][M C NP] Possible null pointer dereference](/default/index/img?u=L2RlZmF1bHQvaW5kZXgvaW1nP3U9TDJsdFlXZGxjeTlrYjNRdVoybG0=)
}
这里不多解释,更正代码的方法很多,可以用try...catch...finally...处理可能出现的异常,也可以在rs.close之前,先判断rs是否为null。
//
ResultSet rs = cmd.executeQuery();
if (rs != null && rs.next()) {
goodsId = rs.getInt("goods_id");
}
rs.close(); //rs可能为null
//
}