user1374266
user1374266

Reputation: 333

I get an error in Sonar-Correctness - Double assignment of field

I am in my early days as a developer and need help to resolve the Sonar error which says double assignment of field for the below code.

//Inside constructor
list1 = new ArrayList();
....
...
// in some method
  if(description.equalsIgnoreCase(MAPPED_CATIA_MODELS)) {
        tempList = new ArrayList();
        tempList = list1 ; // gives error here as "Correctness - Double assignment of field."
        addKey = true;
      }

Please suggest what needs to be changed. Thanks.

Upvotes: 0

Views: 518

Answers (1)

Affe
Affe

Reputation: 47994

Sonar is suggesting you remove one of either

tempList = new ArrayList();

or

tempList = list1;

The point is right after you created an ArrayList, you overwrote the reference to it with a reference to list1. The result of new ArrayList() is immediately thrown away and gone forever.

Upvotes: 2

Related Questions