I'm getting a NullPointerException in these @ModelAttribute variables.
Here is my code:
@RequestMapping(value="/newUser", method=RequestMethod.POST) public String newUser(@Validated @ModelAttribute("user") User user, BindingResult userBindingResult, @Validated @ModelAttribute("credentials") Credentials credentials, BindingResult credentialsBindingResult, @Validated @ModelAttribute("contacts") Contacts contacts, BindingResult contactsBindingResult, @Validated @ModelAttribute("address") Address address, BindingResult addressBindingRusult, Model model) { this.userValidator.validate(user, userBindingResult); this.credentialsValidator.validate(credentials, credentialsBindingResult); this.contactsValidator.validate(contacts, contactsBindingResult); this.addressValidator.validate(address, addressBindingRusult); if (!userBindingResult.hasErrors() && !credentialsBindingResult.hasErrors() && !contactsBindingResult.hasErrors() && !addressBindingRusult.hasErrors()) { user.setContacts(contacts); user.setAddress(address); credentials.setUser(user); credentials.setRole(Credentials.DEFAULT_ROLE); credentials.setPassword(passwordEncoder.encode(credentials.getPassword())); credentials.setEnabled(true); credentialsService.saveCredentials(credentials); return "redirect:/dashboard?successUser"; } else { return "redirect:/dashboard?errorUser"; } } I added to the model these attributes in a previous GET method when the page loads and I checked in Chrome console that data are sent through by the form. Here is part of my code for the GET method:
@RequestMapping(value= {"/dashboard"}, method= RequestMethod.GET) public String loadDashboardPage (Model model) { /*Users signup*/ model.addAttribute("user", new User()); model.addAttribute("credentials", new Credentials()); model.addAttribute("contacts", new Contacts()); model.addAttribute("address", new Address()); return "dashboard"; } Here is the error I got in my Eclipse console:
java.lang.NullPointerException: null at com.applicationSample.App.validator.userValidator.validate(UserValidator.java:46) ~[classes/:na] at com.applicationSample.App.controller.SignupController.newUser(SignupController.java:144) ~[classes/:na] Here you can find the code for the Validator class:
@Override public void validate(Object target, Errors errors) { User user = (User) target; String name = user.getName().trim(); String surname = user.getSurname().trim(); if (name.isEmpty()) { errors.rejectValue("name", "required"); } if (surname.isEmpty()) { errors.rejectValue("surname", "required"); } } I'm using Thymeleaf in my html files to bind data. Also I've already used that approach in a similar method and it's working fine. I can't see where the error could be, any help?