Java NullPointer 自定义验证器 javax.validation.ConstraintValidator

Java NullPointer Custom Validator javax.validation.ConstraintValidator

我有以下用于验证更新请求的验证器。

@Component
public class UpdateDateValidator implements ConstraintValidator<ValidateDateCreditor, BasicUpdateRequest> { 

    @Autowired
    private CreditorRepository creditorRepository;  

    @Override
    public boolean isValid(BasicUpdateRequest object, ConstraintValidatorContext constraintValidatorContext) {          
                                        
        Creditor creditor = creditorRepository.findById(object.getIdCreditor()).orElseThrow(() -> new NoSuchElementException(Constants.ELEMENT_NOT_FOUND_MSG + ": CREDITOR"));
                    
        if (creditor.getDateDeadline() == null 
                || UtilityDate.compareDateNoTime(object.getDateDeadline(), creditor.getDateDeadlineConvention()) <= 0) {
            return true;
        }
    
        return false;
    }

}

object.getIdCreditor() 有一个值, 但是 findById 方法“creditorRepository.findById(object.getIdCreditor())”的执行进入 NullPointer 异常

我不明白这是怎么回事。

我通过添加解决了它:

@Override
public void initialize(ValidateDateCreditor constraintAnnotation) {
    ConstraintValidator.super.initialize(constraintAnnotation);
}