0

I have the same problem that the guy from this thread has. More precisely I get the following error, when trying to inject a bean in my custom validator that implements CustomValidator interface (it's a NPE when accessing the bean i wanted to inject):

javax.validation.ValidationException: HV000028: Unexpected exception during isValid call.
at org.hibernate.validator.internal.engine.ConstraintTree.validateSingleConstraint(ConstraintTree.java:294)
at org.hibernate.validator.internal.engine.ConstraintTree.validateConstraints(ConstraintTree.java:164)
at org.hibernate.validator.internal.engine.ConstraintTree.validateConstraints(ConstraintTree.java:125)
at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:86)

...

Caused by: java.lang.NullPointerException

Do you have a solution for this? Maybe an example? Because I tried the solutions offered on the other thread and nothing worked.

Any help is appreciated. Thanks.

Community
  • 1
  • 1
TheRock3t
  • 628
  • 9
  • 22
  • Post example of your validator together with the `Caused by` stack-trace. Top level exceptions are usually not so interesting. Also include your validator configuration (XML or `@Configuration` bean) – Pavel Horal Jun 13 '13 at 10:27
  • It was Caused by a NPE when accessing data from the bean I wanted to inject in the validator. I use the default validator, so I have no specific XML configuration. This is how I invoke the validator ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); Set> constraintViolations = validator.validate(object); – TheRock3t Jun 13 '13 at 11:09
  • You should probably not use `Validation`. Just `@Autowire` `ValidatorFactory` if you want to create your validator manually. I'll put that into an actual answer :). – Pavel Horal Jun 13 '13 at 11:19

1 Answers1

2

You should not create validator factory on your own (Validation.buildDefaultValidatorFactory()) if you want to use Spring based constraint validators.

You should let Spring autowire correct factory to your bean:

@Controller
public class MyController {

    @Autowired
    private ValidatorFactory validatorFactory;

    // ... handler methods

}
Pavel Horal
  • 17,782
  • 3
  • 65
  • 89
  • Ok. I declared a LocalValidatorFactoryBean bean in the xml file. I wire it as you suggest but I still get NPE when calling the getValidator() method on the wired bean Validator validator = validatorFactory.getValidator(); (here is the NPE) Set> constraintViolations = validator.validate(object); – TheRock3t Jun 13 '13 at 12:09
  • Pavel Horal, any suggestion? – TheRock3t Jun 13 '13 at 14:30
  • Hm... judging on the source code it is impossible to get `null` from `LocalValidatorFactoryBean#getValidator` when using hibernate-validator. Can you share your configuration? – Pavel Horal Jun 13 '13 at 18:31
  • Yep. There is the null. I use maven as build manager, and in my pom I have these dependency declared javax.validation validation-api 1.0.0.GA org.hibernate hibernate-validator 4.3.0.Final runtime – TheRock3t Jun 14 '13 at 05:39
  • Try to debug the `getValidator` call. Spring should call Hibernate's `ValidatorFactoryImpl#getValidator` almost right away, which then should call `ValidatorContextImpl#getValidator`, which **always returns** `ValidatorImpl`. There is no way you can get `null`, unless there is some dependency mess, which you are not aware of, or you have some agressive AOP configuration, or I don't know :). – Pavel Horal Jun 14 '13 at 06:34
  • When I load my application context I get the following error: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.validation.ValidatorFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}. But i declared the LocalValidatorFactoryBean. And I try to inject it in a ValidationFactory instance as you suggested. – TheRock3t Jun 14 '13 at 12:22
  • Can you please share Spring configuration (XML or @Configuration). Where is your validator bean, where are you trying to inject it, ... – Pavel Horal Jun 14 '13 at 13:15