Codeql Scanning Alerts "Dereferenced variable may be null"

35 views Asked by At

In my code I get the above warning. Here is the part of the code where I get it,

@Data
@Builder
public class Employee {
  private String email;
  @Data
  @Builder
  public static class Department{
    @Singular(value = "department")
    private final Set<String> set;
  }
}

Variable set may be null at this access as suggested by null guard.

need to resolve 'set' is not added null value which is not give Code Scanning Alerts "Dereferenced variable may be null"

1

There are 1 answers

0
NoUserNoName On

You used final.

private final Set<String> set;

The final is used that >>value<< or >>ref to the object<< cannot be changed.

To fix your problem init in >>all constructors<< or >>spot<< or delete final.

Option 1

import java.util.*; //there are 8 different Sets to chose from
private final Set<String> set = new HashSet<>();

Option 2

import java.util.*;
public Department(){ set = new HashSet<>(); }

Option 3 ... you still need to call init (set = new HashSet<>())

import java.util.*;
private Set<String> set;

This link might helps you find more about final: https://en.wikipedia.org/wiki/Final_(Java)