4

I have class with final fields. When I validate my fields on the constructor - there is a NullPointerException. I use javax.validation.constraints.NotNull. It is occurs only when I validate fields in the constructor. When I do it earlier - it`s work. Do you know why? //It is working good

public final class User { 

@NotNull private final String name; 
@NotNull private final List<Notes> notes; 

@JsonCreator public User(String name, List<Notes> notes){ 
this.name = name; 
this.notes = notes }



//It isn`t working, NPException 
public final class User { 

private final String name; 
private final List<Notes> notes; 

@JsonCreator 
public User(@NotNull String name, @NotNull List<Notes> notes){ 
this.name = name; 
this.notes = notes }
michf
  • 209
  • 6
  • 17
  • 1
    What do you think these annotations do - exactly I mean? – GhostCat Oct 03 '17 at 18:11
  • Check if field is null and then (if it is null) show nice validation error with messagge about null field. On https://docs.oracle.com/javaee/7/tutorial/bean-validation003.htm there are a samples with using it in the constructor – michf Oct 03 '17 at 18:17
  • 2
    Are you running your application in an environment with a validator that actually checks those annotations? Otherwise, they're just text which does nothing except communicating intended use to programmers. – iheanyi Oct 03 '17 at 18:25
  • I just run it on spring boot app and it works only in first example (@NotNull before field declarations) – michf Oct 03 '17 at 19:50
  • have you been able to get the second version to work? – Ahmed Abdelhady Jun 20 '19 at 07:51

0 Answers0