The parameter is explicitly annotated as being Nullable. Either the use of the parameter or the annotation is wrong

65 Views Asked by At

When below code is run for bugs in Sonarqube

@Component
public class SMOCreateSignObjectValidator implements Validator{

private static final Logger LOG = LoggerFactory.getLogger(SMOCreateSignObjectValidator.class);
@Override
public boolean supports(Class<?> clazz) {
    LOG.info("Inside create Sign Object validator");
    return AgreementRecordRequest.class.isAssignableFrom(clazz);

}

@Override
public void validate(Object target, Errors errors) {

    AgreementRecordRequest agreementRecordRequest = (AgreementRecordRequest)target;
    if(null==agreementRecordRequest.getCustomerId() || agreementRecordRequest.getCustomerId().replaceAll("\"", "").length()==0 ){
        throw new SystemException(SMOErrorConstants.MESSAGE_SMO_0003_MSG);
    }
    if(agreementRecordRequest.getExpiryDate().isEmpty()){
        throw new SystemException(SMOErrorConstants.MESSAGE_SMO_0004_MSG); 
    }
}

}

the following error is thrown in sonarcube. sonarqube screenshot

"target must be non-null but is marked as nullable"

I have not explicitly added any annotation,Why am i getting this error?

1

There are 1 best solutions below

2
olgunyldz On

You are assigning target parameter by class-casting to other reference. For this reason, you might get an exception. You have to control it which is null or not null or you have to add notnull annotation.