使用 findbugs 扫描以下代码后,它会报告 Dodgy code:NP: Load of known null value in new ....(at line where new Exception isthrowed)
有时需要在初始化对象之前检查 null。
为什么这被认为是“狡猾的”?
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 指出您抛出异常的那一行
throw new EmpServiceException(aDepartment, "Invalid Request");
相当于
throw new EmpServiceException(null, "Invalid Request");
并希望您使用后者。是第一个参数
EmpServiceException
用 @NonNull
注释的构造函数?
关于constructor - Findbugs 在验证构造函数参数时报告已知空值的负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33167640/