I am using Sonar and I have got this kind of violation from it for a peace of my code:
Correctness - Possible null pointer dereference
Has a
I got this issue with the following piece of code:-
BufferedReader br = null;
String queryTemplate = null;
try {
br = new BufferedReader(new FileReader(queryFile));
queryTemplate = br.readLine();
} catch (FileNotFoundException e) {
// throw exception
} catch (IOException e) {
// throw exception
} finally {
br.close();
}
Here, the br
BufferedReader can be null
in br.close()
. However it can only be null
if new BufferedReader()
fails, in which case we are throwing the relevant exceptions.
This is thus a false warning. Findbugs
docs mention the same:-
This may lead to a NullPointerException when the code is executed.
Note that because FindBugs currently does not prune infeasible
exception paths, this may be a false warning.