0

i need help I'm trying to create a filter for a store project, using spring, thymeleaf and bootstrap in which the filter will receive from the Product class the value of the "status" attribute which is an enum, however when using the expression language "${produto.status.name()}" it returns me the exception of the topic I will post the code of the html page, the model class and the enum.

HTML snippet

<main>
        <div class="container" >
            
        
            <h2 class="text-center">Produtos</h2>
        
                    <div class="row">
                        <div class=" d-flex align-items-stretch" style="flex-direction: row; flex-wrap: wrap; justify-content:center; ">
                    
                            <div class=" col-md-4 col-xl-3 col-lg-3 col-sm-6 mx-1 my-2" th:each="produto : ${produtos}" >
                                <th:block th:switch="${produto.status.name()}">
                                    <div th:case="'ACESSORIOS'" class="card border-dark bg-dark text-white text-center"></div>
                                    <div th:case="'CELULAR'" class="card border-primary bg-dark text-white text-center">></div>
                                    <div th:case="'COMPUTADOR'" class="card border-light bg-dark text-white text-center"></div>
                                </th:block>

Product class

@Entity
public class Produto {
    
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String nome;
    private BigDecimal preco;
    private Integer quantidade;
    private String descricao;
    private String urlImagem;
    
    
    @Enumerated(EnumType.STRING)
    private Statuscategoria status;

    public Statuscategoria getStatus() {
        return status;
    }
    public void setStatus(Statuscategoria status) {
        this.status = status;
    }
    ... other getters and setters

Enum code

public enum Statuscategoria {
 CELULAR, COMPUTADOR, ACESSORIOS;

 }

This is the error Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method name() on null context object

  • ${produto.status.name()} - In this, "status" object is null may be. Please check. – Abhale Amol Jun 21 '22 at 10:08
  • Either `produto` or `status` is `null`, if this is an expected option you could do `${produto.?status.?name()}` to be null safe and implement a `th:case="*"` as a fallback if needed in view. If this is not what you expected should debug why the object or paramaeter is null which can't be seen from code given here – Ralan Jun 21 '22 at 14:34

0 Answers0