2015-10-16 29 views
4

使用findbugs扫描以下代码时,它会报告Dodgy代码:NP:在新的...中加载已知的空值(在引发新异常的行处)Findbugs报告在验证构造函数参数时加载已知的空值

有时需要在初始化对象之前检查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; 
    } 

回答

4

我的猜测是,FindBugs的是指出,在那里你抛出异常

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

行相当于

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

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

+0

谢谢,就是这样。 –