1

I have a method like:

public String getParamValue(@NotNull String param) {
        .......
        .......
        .......
    }

Even after putting @NotNull in-front of the param, whenever i am calling getParamValue(null) it is not throwing NPE. It proceeds as normal, do i need to do something else or am i using it wrongly? Thanks.

I am using Java 7 and javax.validation.constraints.NotNull if it helps in any ways.

Trying
  • 14,004
  • 9
  • 70
  • 110

3 Answers3

3

This annotation doesn't do anything by itself. It is just a mark for other tools, so they know the constraints. The tools that checks it are source code analyzers and validation tools.

Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
1

This is not a garantee for notnull, its more like a promise. so you could do a preconditions check:

if (param == null) {
    throw new PreconditionExc...
MemLeak
  • 4,456
  • 4
  • 45
  • 84
0

Hi this question has already been asked before. To summarize you will need to do this:

MVC namespace configuration for annotations:

The JSR-303 spec JAR: validation-api-1.0.0.GA.jar (looks like you already have that)

An implementation of the spec, such as Hibernate Validation, which appears to be the most commonly used example: hibernate-validator-4.1.0.Final.jar you could also use one of these https://code.google.com/p/gwt-validation/ and http://bval.apache.org/

In the bean to be validated, validation annotations, either from the spec JAR or from the implementation JAR (which you have already done)

In the handler you want to validate, annotate the object you want to validate with @Valid, and then include a BindingResult in the method signature to capture errors.

Annotations from javax.validation.constraints not working

Community
  • 1
  • 1
Arno_Geismar
  • 2,296
  • 1
  • 15
  • 29