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

前端 未结 5 705
悲哀的现实
悲哀的现实 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:42

    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 ?

提交回复
热议问题