I can not understand the reason for this error. It is written Caused by NullPointerException , in a string with hash code. What, in principle, leads to an error?
I initialize nickName, password, email: When creating a User, the rest will be Null, maybe the problem appears when generating the hash code to null? why?
public static void main(String [] strings){ ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); Validator validator = validatorFactory.getValidator(); Set<ConstraintViolation<User>> constraints = validator.validate(new User("Stas","StasPassword","Stas.Stanis88@gmail.com")); for (ConstraintViolation<User> constraint:constraints) { System.out.println(constraint.getMessage()); } public class User { public String getFirstName() { return firstName; } private void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } private void setLastName(String lastName) { this.lastName = lastName; } public String getPassword() { return password; } private void setPassword(String password) { this.password = password; } public String getEmail() { return email; } private void setEmail(String email) { this.email = email; } public String getCustomerType() { return customerType; } private void setCustomerType(String customerType) { this.customerType = customerType; } public String getNickName() { return nickName; } private void setNickName(String nickName) { this.nickName = nickName; } public User(){} public User(String nickName,String password, String email){ setEmail(email); setNickName(nickName); setPassword(password); } public User(String firstName, String lastName, String nickName ,String email, String password, String customerType) { setCustomerType(customerType); setEmail(email); setFirstName(firstName); setLastName(lastName); setNickName(nickName); setPassword(password); } @Override public String toString() { return "User{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + ", customerType='" + customerType + '\'' + ", nickName='" + nickName + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (!getFirstName().equals(user.getFirstName())) return false; if (!getLastName().equals(user.getLastName())) return false; if (!getPassword().equals(user.getPassword())) return false; if (!getEmail().equals(user.getEmail())) return false; if (!getCustomerType().equals(user.getCustomerType())) return false; return getNickName().equals(user.getNickName()); } @Override public int hashCode() { 107: int result = getFirstName().hashCode(); // Проблемма возникает здесь! result = 31 * result + getLastName().hashCode(); result = 31 * result + getPassword().hashCode(); result = 31 * result + getEmail().hashCode(); result = 31 * result + getCustomerType().hashCode(); result = 31 * result + getNickName().hashCode(); return result; } @NotEmpty(message = "This field can't be empty") @NotBlank(message = "This field can't be empty") @Length(min = 0,max = 20,message = "The length of this field can be only 0-20") private String firstName; @NotEmpty(message = "This field can't be empty") @NotBlank(message = "This field can't be empty") @Length(min = 0,max = 20,message = "The length of this field can be only 0-20") private String lastName; @NotEmpty(message = "This field can't be empty") @NotBlank(message = "This field can't be empty") @Length(min = 8,max = 16,message = "Wrong password length: length of this field can be only 8-16") private String password; @NotEmpty(message = "This field can't be empty") @NotBlank(message = "This field can't be empty") @Email(message = "Wrong format for email field!" , regexp = "[A-Za-z.0-9]+@[A-Za-z.]+\\.[az]{2,4}") private String email; @NotEmpty(message = "This field can't be empty") @NotBlank(message = "This field can't be empty") private String customerType; @NotEmpty(message = "This field can't be empty") @NotBlank(message = "This field can't be empty") @Length(min = 4,max = 10,message = "Wrong nickName: The length of this field can be only between 4-10") private String nickName; } Exception in thread "main" javax.validation.ValidationException: HV000041: ***Call to TraversableResolver.isReachable() threw an exception.*** at org.hibernate.validator.internal.engine.ValidatorImpl.isReachable(ValidatorImpl.java:1405) at org.hibernate.validator.internal.engine.ValidatorImpl.isValidationRequired(ValidatorImpl.java:1381) at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:542) at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:487) at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:451) at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:403) at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:206) at UserValidationTest.main(UserValidationTest.java:16) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) ***Caused by: java.lang.NullPointerException at coupon.beans.User.hashCode(User.java:107)*** at org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation$TraversableHolder.buildHashCode(CachingTraversableResolverForSingleValidation.java:153) at org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation$TraversableHolder.<init>(CachingTraversableResolverForSingleValidation.java:114) at org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation$TraversableHolder.<init>(CachingTraversableResolverForSingleValidation.java:96) at org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation.isReachable(CachingTraversableResolverForSingleValidation.java:41) at org.hibernate.validator.internal.engine.ValidatorImpl.isReachable(ValidatorImpl.java:1396) ... 12 more public class User { public String getFirstName() { return firstName; } private void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } private void setLastName(String lastName) { this.lastName = lastName; } public String getPassword() { return password; } private void setPassword(String password) { this.password = password; } public String getEmail() { return email; } private void setEmail(String email) { this.email = email; } public String getCustomerType() { return customerType; } private void setCustomerType(String customerType) { this.customerType = customerType; } public String getNickName() { return nickName; } private void setNickName(String nickName) { this.nickName = nickName; } public User(){} public User(String nickName,String password, String email){ setEmail(email); setNickName(nickName); setPassword(password); } public User(String firstName, String lastName, String nickName ,String email, String password, String customerType) { setCustomerType(customerType); setEmail(email); setFirstName(firstName); setLastName(lastName); setNickName(nickName); setPassword(password); } @Override public String toString() { return "User{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + ", customerType='" + customerType + '\'' + ", nickName='" + nickName + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (!getFirstName().equals(user.getFirstName())) return false; if (!getLastName().equals(user.getLastName())) return false; if (!getPassword().equals(user.getPassword())) return false; if (!getEmail().equals(user.getEmail())) return false; if (!getCustomerType().equals(user.getCustomerType())) return false; return getNickName().equals(user.getNickName()); } // Новый hash code метод! @Override public int hashCode() { int result = getFirstName() == null ? 0:getFirstName().hashCode(); result = 31 * result + getLastName() == null ? getLastName().hashCode():0; result = 31 * result + getPassword().hashCode(); result = 31 * result + getEmail().hashCode(); result = 31 * result + getCustomerType() == null ? getCustomerType().hashCode():0; result = 31 * result + getNickName().hashCode(); return result; } @NotEmpty(message = "This field can't be empty") @NotBlank(message = "This field can't be empty") @Length(min = 0,max = 20,message = "The length of this field can be only 0-20") private String firstName; @NotEmpty(message = "This field can't be empty") @NotBlank(message = "This field can't be empty") @Length(min = 0,max = 20,message = "The length of this field can be only 0-20") private String lastName; @NotEmpty(message = "This field can't be empty") @NotBlank(message = "This field can't be empty") @Length(min = 8,max = 16,message = "Wrong password length: length of this field can be only 8-16") private String password; @NotEmpty(message = "This field can't be empty") @NotBlank(message = "This field can't be empty") @Email(message = "Wrong format for email field!" , regexp = "[A-Za-z.0-9]+@[A-Za-z.]+\\.[az]{2,4}") private String email; @NotEmpty(message = "This field can't be empty") @NotBlank(message = "This field can't be empty") private String customerType; @NotEmpty(message = "This field can't be empty") @NotBlank(message = "This field can't be empty") @Length(min = 4,max = 10,message = "Wrong nickName: The length of this field can be only between 4-10") private String nickName; } public static void main(String [] strings){ ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); Validator validator = validatorFactory.getValidator(); User user = new User("Stas","StasPassword","Stas.Stanis88@gmail.com"); System.out.println(user); Set<ConstraintViolation<User>> constraints = validator.validate(user); for (ConstraintViolation<User> constraint:constraints) { System.out.println(constraint.getMessage()); } Output to console after validation check:
This is after printing the initialized object.
User{firstName='null', lastName='null', password='StasPassword', email='Stas.Stanis88@gmail.com', customerType='null', nickName='Stas'} This is the validation of this object:
This field can't be empty This field can't be empty This field can't be empty This field can't be empty This field can't be empty This field can't be empty Null Pointer Exception disappeared, but the validator treats the fields of the object as null, why ??