0

I'm getting this exceptions when trying to update an object with a PostMapping:

Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "libro.id" (template: "formulario-modificar-libros-p" - line 18, col 15)

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'id' cannot be found on null

Form:

<form th:action="@{/libros/modificar-libro-d/__${libro.id}__}" method="post">
     <input hidden th:value="${libro.id}" name="id">
     <div class="form-group">
         <label>ISBN del libro</label> <input th:value="${libro.isbn}" type="number" class="form-control" name="isbnLibro" required>   
     </div>
     <div class="form-group">
         <label>Título del libro</label> <input th:value="${libro.titulo}" type="text" class="form-control" name="tituloLibro" required>
     </div>
     <div class="form-group">
         <label>Año del libro</label> <input th:value="${libro.anio}" type="number" class="form-control" name="anioLibro">
     </div>
     <div class="form-group">
         <label>Ejemplares del libro</label> <input th:value="${libro.ejemplares}" type="number" class="form-control" name="ejemplaresLibro" required>
     </div>
     <div>
         <label>Autor</label>
         <select class="form-select" aria-label="Default select example" name="nombreAutor">
             <option th:each="autor:${autores}"
                     th:text="${autor.nombre}"
                     th:value="${autor.nombre}"
             >    
             </option>
         </select>
     </div>
     <div>
         <label>Editorial</label>
         <select class="form-select" aria-label="Default select example" name="nombreEditorial">
             <option th:each="editorial:${editoriales}"
                     th:text="${editorial.nombre}"
                     th:value="${editorial.nombre}"
             >    
             </option>
         </select>
     </div>
     <button type="submit" class="btn btn-primary">Aceptar</button>
 </form>

Controller

@PostMapping("/modificar-libro-d/{id}")
public String modificarLibroM(@PathVariable String id, @RequestParam Long isbnLibro, @RequestParam String tituloLibro, @RequestParam Integer anioLibro,
        @RequestParam Integer ejemplaresLibro, @RequestParam String nombreAutor, @RequestParam String nombreEditorial){
    try {
        libroServicio.modificarLibro(id, isbnLibro, tituloLibro, anioLibro, ejemplaresLibro, nombreAutor, nombreEditorial);

        return "redirect:/libros/lista-libros-d";
    } catch (Exception e) {
        return "formulario-modificar-libros-p";
    }
}

@GetMapping("/modificar-libro-d/{id}")
public String formularioModificarLibrosM(@PathVariable String id, ModelMap modelo) {
    modelo.addAttribute("libro", libroServicio.buscarLibroPorId(id));
    modelo.addAttribute("autores", autorServicio.listarAutoresAlta());
    modelo.addAttribute("editoriales", editorialServicio.listarEditorialesAlta());
        
    return "formulario-modificar-libros-p";
}

In the entity, all fields have their getters and setters correctly.

sixGdie
  • 1
  • 2
  • Does this answer your question? [EL1007E: Property or field 'fieldName' cannot be found on null](https://stackoverflow.com/questions/53462818/el1007e-property-or-field-fieldname-cannot-be-found-on-null) – geco17 Nov 13 '21 at 19:11
  • Hi, this looks like a duplicate. The problem is not how your entity is defined, it's that it doesn't look like a model attribute exists for "libro" – geco17 Nov 13 '21 at 19:12
  • I think i'm adding the attribute "libro" in the @GetMapping, post edited – sixGdie Nov 13 '21 at 19:37

1 Answers1

0

It looks like your code is okay but the find by id method on your book service returns null for the given id. Put a breakpoint on it and run it in debug. If a libro object must always be present, consider defining your libroServicio.buscarLibroPorId(id) method such that an exception is thrown for a missing id and/or set up error handling in your thymeleaf template.

geco17
  • 5,152
  • 3
  • 21
  • 38