我有这段代码
private void doSomething(Properties properties) throws XXException
{
PersistenceManager pm = null;
try {
pm = new PersistenceManager(distributedDatabase, "PeriodHelper: insertPeriod");
try {
pm.create(Tables.MY_TABLE);
} catch (ObjectAlreadyExistsException e) {
logger.warn("Could not create table! - "+e.getMessage(), e);
return;
}
pm.commit();
return;
}
catch (Exception e) {
throw new XXException(e.getMessage(), e);
}
finally {
if (pm != null)
pm.close();
}
}
,findbugs / sonar报告受到
的影响Correctness - close() invoked on a value that is always null
close() is being invoked on a value that is always null. If this statement
is executed, a null pointer exception will occur. But the big risk here you
never close something that should be closed.
Key: NP_CLOSING_NULL
但是如果查看代码,调用close()方法时的pm对象将始终不为null。任何人都可以解释为什么findbugs错了吗?
答案 0 :(得分:1)
要么 Findbugs是完全错误的(不是第一次)或它知道pm
在这里不能为空(在在哪种情况下,它会报告错误的错误,应该说“死代码”或“条件总是假的”。)
答案 1 :(得分:-1)
您是否尝试使用大括号括起if语句?
if (pm!=null) {pm.close()};