开发者

Findbugs - "close() invoked on a value that is always null"

I have this block of code

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();
    }
}

that findbugs/sonar is reporting is afflicted by

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

but if you look at the code, the pm object when the close() method is called will always be not null. Can anyone explain why findbugs is getting this wrong?


Either Findbugs is simply wrong (wouldn't be the first time) or it knows that pm can't be non-null here (in which case it would kind-of report the wrong error, it should say "dead code" or "condition is always false").


Do you try to enclose the if statment using braces?

if (pm!=null) {pm.close()};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜