I don`t know why, when I POST the empty form I have that error
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'addresses' cannot be found on null
before I POST the form:
@GetMapping("/new")
public String showCreateFormForEmployeeAndAddresses(Model model) {
if(addressService.getCheck()== false) {
for (int i = 1; i <= 2; i++) {
addressService.newAddress(new Address());
}
}
model.addAttribute("employee", new Employee()).addAttribute("form", addressService);
return "new_employee_form";
}
After I POST the empty form: (in filled form everything work fine)
@RequestMapping(value = "/employees", method = RequestMethod.POST)
public String saveEmployeeAndAddress(@ModelAttribute @Valid Employee employee,
BindingResult bindingResultEmployee,
@ModelAttribute @Valid AddressRepository addresses,
BindingResult bindingResultAddressRepository, Model model) {
if(bindingResultEmployee.hasErrors() || bindingResultAddressRepository.hasErrors()) {
return "new_employee_form";
} else{
ExecutorService executor = Executors.newSingleThreadExecutor();
Runnable runnableEmployee = () -> employeeService.saveEmployeeToDB(employee);
List<Address> addressesFromForm = addresses.getAddresses();
Runnable runnableAddress = () -> addressService.saveAddressToDB(addressesFromForm);
executor.submit(runnableEmployee);
executor.submit(runnableAddress);
return "redirect:/employees";
}
}
It show after this line: return "new_employee_form";
In Console I have also:
[THYMELEAF][http-nio-8080-exec-2] Exception processing template "new_employee_form": Exception evaluating SpringEL expression: "addresses" (template: "new_employee_form" - line 57, col 30)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'addresses' cannot be found on null
and the view:
<div th:each="address, stat : *{addresses}">
<span th:switch="*{addresses[__${stat.index}__].type}">
<div style = "text-align: center;" th:case="P" ><h5>Adres stały</h5></div>
<div style = "text-align: center;" th:case="C" ><h5>Adres korespondencyjny</h5></div>
</span>
<div class="form-group">
<input type="hidden" class="form-control" th:field="*{addresses[__${stat.index}__].type}"/>
</div>
<div class="form-group">
<input type="text" class="form-control" th:field="*{addresses[__${stat.index}__].street}"/>
<label class="control-label">Ulica</label>
<div class="text-danger"><p th:if="${#fields.hasErrors('addresses[__${stat.index}__].street')}" th:errors="*{addresses[__${stat.index}__].street}"/></div>
</div>
<div class="form-group">
<input type="text" class="form-control" th:field="*{addresses[__${stat.index}__].streetNr}"/>
<label class="control-label">Numer domu</label>
<div class="text-danger"><p th:if="${#fields.hasErrors('addresses[__${stat.index}__].streetNr')}" th:errors="*{addresses[__${stat.index}__].streetNr}"/></div>
</div>
<div class="form-group">
<input type="number" class="form-control" th:field="*{addresses[__${stat.index}__].flatNr}"/>
<label class="control-label">Numer mieszkania</label>
<div class="text-danger"><p th:if="${#fields.hasErrors('addresses[__${stat.index}__].flatNr')}" th:errors="*{addresses[__${stat.index}__].flatNr}"/></div>
</div>
<div class="form-group">
<input type="text" class="form-control" th:field="*{addresses[__${stat.index}__].postalCode}"/>
<label class="control-label">Kod pocztowy</label>
<div class="text-danger"><p th:if="${#fields.hasErrors('addresses[__${stat.index}__].postalCode')}" th:errors="*{addresses[__${stat.index}__].postalCode}"/></div>
</div>
<div class="form-group">
<input type="text" class="form-control" th:field="*{addresses[__${stat.index}__].city}"/>
<label class="control-label">Miasto</label>
<div class="text-danger"><p th:if="${#fields.hasErrors('addresses[__${stat.index}__].city')}" th:errors="*{addresses[__${stat.index}__].city}"/></div>
</div>
<div class="form-group">
<input type="text" class="form-control" th:field="*{addresses[__${stat.index}__].country}"/>
<label class="control-label">Kraj</label>
<div class="text-danger"><p th:if="${#fields.hasErrors('addresses[__${stat.index}__].country')}" th:errors="*{addresses[__${stat.index}__].country}"/></div>
</div>
</div>