20

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 anyone know about this rule in findbugs? I searched a lot but I can not find a good sample code (in Java) which describe this rule, unfortunately findbugs site did not have any sample code or good description about this rule.

Why does this violation appear?

Saeed Zarinfam
  • 9,818
  • 7
  • 59
  • 72

5 Answers5

20

a sample code is something like this.

String s = null ;
if (today is monday){
    s = "Monday" ;
else if (today is tuesday){
    s = "Tuesday" ;
}
System.out.println(s.length()); //Will throw a null pointer if today is not monday or tuesday.
Balaji Natesan
  • 791
  • 6
  • 4
18

It says here

NP: Possible null pointer dereference (NP_NULL_ON_SOME_PATH)

There is a branch of statement that, if executed, guarantees that a null value will be dereferenced, which would generate a NullPointerException when the code is executed. Of course, the problem might be that the branch or statement is infeasible and that the null pointer exception can't ever be executed; deciding that is beyond the ability of FindBugs.

If you would have posted some code it would be easier to answer.

EDIT I don't see a lot of documentation but here is one example! Hope this helps!

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
  • My code is very complicated and also i changed my code and it fixed but i did not understand this findbugs rule. – Saeed Zarinfam Sep 03 '12 at 05:29
  • Well than as it says `There is a branch of statement that, if executed, guarantees that a null value will be dereferenced, which would generate a NullPointerException when the code is executed.` means you potentially are assigning a `null` to a variable and using it again which might cause an exception. – Bharat Sinha Sep 03 '12 at 05:31
  • Please give me a sample code. I read this description before ! – Saeed Zarinfam Sep 03 '12 at 06:19
3

Okay

This is two simple Examples : First one gives a : Possible null pointer dereference

1. Error
     ArrayList a = null;
     a.add(j, PointSet.get(j));
     // now i'm trying to add to the ArrayList 
     // because i'm giving it null it gives me the "Possible null pointer dereference"

2. No Error
     ArrayList a = new ArrayList<>();
     a.add(j, PointSet.get(j));
     // adding elements to the ArrayList
     // no problem

Simple ?

David Rodrigues
  • 12,041
  • 16
  • 62
  • 90
ucefkh
  • 2,509
  • 2
  • 22
  • 18
1

In simple language, if a variable value is assigned as null, and you try to access it with any inbuilt method like add/get. Then null pointer dereference issue comes with SONAR. Because there are changes for it go null, and throw null pointer exception. Try to avoid it if possible.

For example:

File file=null;
file.getName();

will throw "Possible null pointer dereference"

It may not happen directly as mentioned in the example, it can be unintentionally.

Laurel
  • 5,965
  • 14
  • 31
  • 57
1

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.
Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
Sanchay
  • 1,053
  • 1
  • 16
  • 33
  • 1
    ... and since you're using a finally block, you're guaranteeing that, after the exception from the bufferedreader is caught and (probably) rethrown, a nullpointer occurs... – Koos Gadellaa Jun 12 '18 at 09:58