Search code examples
javaeclipse-pluginnullpointerexceptionjtest

JTest - variable may possibly be null in try catch block


I have the code as below:

object a = getObjectFromBlahBlah(); //'a' may possibly be null

try{
    a.beginTransaction();
    .
    .
    .

} catch (Exception e) {
    logger.debug(e.getMessage());
}

After I run JTest, it will warn me 'a' may possibly be null. I don't want to do one more checking like if (a == null) return; since I already cover the NullPointerException in the try/catch block. Can anyone explain what happen to this? How I can get rid of this warning from JTest by alter the code?


Solution

  • How I can get rid of this warning from JTest by alter the code?

    You said it yourself: you need to check a == null. JTest doesn't care that you "already cover the NullPointerException" in the current configuration. I don't know if it can be set up to recognize this, but presumably you also aren't allowed to change its configuration, given

    that JTest rules is setup for code review and audit purpose, can't simply ignore it:)

    If it's acceptable for the audit, you can also suppress the message directly and include the reason.

    Also, I hope

    catch (Exception e) {
        logger.debug(e.getMessage());
    }
    

    is just a simplification and wouldn't pass any code review or audit!