Findbugs在验证构造函数参数时报告已知空值的加载

Findbugs reports Load of known null value while validating constructor argument

在使用findbugs扫描以下代码时,它报告了Dodgy代码:NP:在新....(抛出新异常的行)处加载了已知的空值。

有时需要在初始化对象之前检查null。
为什么这被视为"狡猾"?

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Employee{

  @Valid
  private Department dept;

  @JsonCreator
  public Employee(@JsonProperty(value ="department", required = true) Department aDepartment)
      throws EmpServiceException{
    if (aDepartment == null) {
      throw new EmpServiceException(aDepartment,"Invalid Request");
    }
    this.dept= aDepartment;
  }

我的猜测是FindBugs指出了引发异常的行

1
throw new EmpServiceException(aDepartment,"Invalid Request");

等同于

1
throw new EmpServiceException(null,"Invalid Request");

,并希望您使用后者。该EmpServiceException构造函数的第一个参数是否用@NonNull注释?