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?
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.