Findbug shows that
There is a statement or branch on an exception path that if executed guarantees that a value is null at this point, and that value that is guaranteed to be dereferenced (except on forward paths involving runtime exceptions).
Possible scenario/use case/cases are like this.
1.) When branching is present in the code and we suppose to initialize object in branch and accessing that object(without null check) out of the branch.
As user3227602 shows a code, You have declared sessionFactory object as NULL object and initialize it in a try-catch-finally block.
Imagine a scenario that before initializing, if exception occurs(due to some processing) then flow will come in catch - finally block. So sessionFactory still only declared as a NULL object. So Null check is advisable.
2.) When some statement (if-elseif-else) is present in the code and we suppose to initialize object in that block(if block, elseif block or in else block) and accessing that object(without null check).
if(condition1)
{
//blah blah
sessionFactory = new Configuration().configure().buildSessionFactory();
//blah blah
}
else
{
//blah blah
}
if (!sessionFactory.isClosed()) {
session.close();
sessionFactory.close();
}
If result of the condition1 is false then your sessionFactory object will never initilize.
so NULL check is required for this.