0

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>
Bartek K
  • 41
  • 5
  • 1
    this might help [this](https://stackoverflow.com/questions/47406346/org-springframework-expression-spel-spelevaluationexception-el1007e-property-o) – user404 May 19 '20 at 16:59

1 Answers1

0

Returned view new_employee_form requires an addresses attribute to be supplied to it.

It looks like @ModelAttribute @Valid AddressRepository addresses argument is null, so it has to be provided manually:

if(bindingResultEmployee.hasErrors() || bindingResultAddressRepository.hasErrors()) {
    model.addAtribute("addresses", an_instance_of_whatever_addresses_is)
    return "new_employee_form";
}

Looking at the given code, I would expect that the Getter method (showCreateFormForEmployeeAndAddresses(Model model)) provides it to the model (otherwise it's validation would always null-fail).

In any case, I would highly recommend you to reevaluate the way you name your model attribute classes and variables, as names like AddressRepository or addressService have totally different meaning in Spring MVC.

MartinBG
  • 1,500
  • 13
  • 22
  • Thanks for help. I add model Atribute manualy. Now there is no error. The employee form is validated but addresses are unfortunately not. "an_instance_of_whatever_addresses_is" addresses is a LIST
    list od two adresses. So currently before return I have model.addAttribute("employee", employee).addAttribute("form", addresses); and view https://zapodaj.net/b7841cb1fdb73.png.html
    – Bartek K May 19 '20 at 18:29