I’m using Spring Boot to save an image but after filling out the form and loading the image, ¿Why do I have the next error after clicking the bootom “save” (guardar)?:
Cannot invoke “com.ecomerce.service.UploadFileService.saveImage(org.springframework.web.multipart.MultipartFile)” because “this.upload” is null
This is the code in the controller:
@PostMapping("/save")
public String save(Producto producto, @RequestParam("img") MultipartFile file) throws IOException {
LOGGER.info("--- Info para guardar producto: {}", producto);
LOGGER.info("--- Datos de la imagen: {}", file);
Usuario u = new Usuario(1,"","","","","","","");
producto.setUsuario(u);
//Para guardar la imagen
//Validación cuando el producto es cargado por primera vez y nunca habŕa imagen
if (producto.getId()==null) { // cuando se crea un producto
String nombreImagen= upload.saveImage(file);
producto.setImagen(nombreImagen);
}else {
}
productoService.save(producto);
return "redirect:/productos";
}
And this is the form in HTML:
<h2>Crear Producto</h2>
<form class="form-horizontal" th:action="@{/productos/save}" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label col-sm-2" for="nombre">Nombre:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="nombre" name="nombre"
placeholder="Ingrese el nombre del producto" autocomplete="off" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="descripcion">Descripción:</label>
<div class="col-sm-10">
<textarea class="form-control" id="descripcion" name="descripcion"
placeholder="Ingrese la descripcion del producto" required
autocomplete="off"> </textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="cantidad">Cantidad:</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="cantidad"
name="cantidad" placeholder="Ingrese la cantidad del producto"
required autocomplete="off">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="precio">Precio:</label>
<div class="col-sm-10">
<input type="number" class="form-control" step="any" id="precio" name="precio"
placeholder="Ingrese el precio del producto" autocomplete="off" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="img">Imagen:</label>
<div class="col-sm-10">
<input type="file" class="form-control-file" id="img" name="img">
</div>
</div>
<div class="row">
<div class="col-sm-2">
<button type="submit" class="btn btn-success">
<span class="glyphicon glyphicon-saved"></span> Guardar
</button>
</div>
</div>
</form>
And console shows the next errors:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat May 25 20:52:39 CST 2024
There was an unexpected error (type=Internal Server Error, status=500).
Cannot invoke “com.ecomerce.service.UploadFileService.saveImage(org.springframework.web.multipart.MultipartFile)” because “this.upload” is null
java.lang.NullPointerException: Cannot invoke “com.ecomerce.service.UploadFileService.saveImage(org.springframework.web.multipart.MultipartFile)” because “this.upload” is null
at com.ecomerce.control.ProductoController.save(ProductoController.java:54)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
I’m using Spring Boot 3.3
I’ve already verified that I wrote enctype="multipart/form-data
in the form.
Please, Can anyone help me? Thank you in advance for your help