What is the meaning of Possible null pointer dereference in findbug?

前端 未结 5 707
悲哀的现实
悲哀的现实 2021-02-07 11:00

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

5条回答
  •  面向向阳花
    2021-02-07 11:29

    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.
    

提交回复
热议问题