2
respostas

Erro 500 referente a EL livroBean.autores

Olá, estou com erro tendo uma mensagem de erro após tentar fazer a Lista de autores para aparecer na minha view, ao iniciar o servidor estou me deparando com esse erro

javax.el.ELException: /livro.xhtml @36,59 value="#{livroBean.autores}": Error reading [autores] on type [br.com.caelum.livraria.bean.LivroBean]

Abaixo segue o xhtml do livro juntamente com a classe LivroBean

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

    <h:body>
        <h1>Novo Livro</h1>
        <h:form>
            <fieldset>
                <legend>Dados do Livro</legend>
                <h:panelGrid columns="2">

                    <h:outputLabel value="Titulo:" for="titulo" />
                    <h:inputText id="titulo" value="#{livroBean.livro.titulo}" />

                    <h:outputLabel value="ISBN:" for="isbn" />
                    <h:inputText id="isbn" value="#{livroBean.livro.isbn}" />

                    <h:outputLabel value="Preço:" for="preco" />
                    <h:inputText id="preco" value="#{livroBean.livro.preco}" />

                    <h:outputLabel value="Data de Lançamento:" for="dataLancamento" />
                    <h:inputText id="dataLancamento"
                        value="#{livroBean.livro.dataLancamento}" />

                </h:panelGrid>
            </fieldset>

            <fieldset>
                <legend>Dados do Autor</legend>

                <h:outputLabel value="Selecione Autor:" for="autor" />
                <h:selectOneMenu value="#{livroBean.autorId}">
                    <f:selectItems value="#{livroBean.autores}" var="autor"
                        itemLabel="#{autor.nome}" itemValue="#{autor.id}" />
                </h:selectOneMenu>
                <h:commandButton value="Gravar Autor"
                    action="#{livroBean.gravarAutor}" />

                <h:dataTable value="#{livroBean.autoresDoLivro}" var="autor">
                    <h:column>
                        <h:outputText value="#{autor.nome}" />
                    </h:column>
                </h:dataTable>
            </fieldset>

            <h:commandButton value="Gravar" action="#{livroBean.gravar}" />

        </h:form>

        <h:dataTable value="#{livroBean.livros}" var="livro">
            <h:column>
                <f:facet name="header">Titulo</f:facet>
                <h:outputText value="#{livro.titulo}" />
            </h:column>
            <h:column>
                <f:facet name="header">ISBN</f:facet>
                <h:outputText value="#{livro.isbn}" />
            </h:column>
            <h:column>
                <f:facet name="header">Preço</f:facet>
                <h:outputText value="#{livro.preco}" />
            </h:column>
        </h:dataTable>

    </h:body>

</html>

Classe

package br.com.caelum.livraria.bean;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import br.com.caelum.livraria.dao.DAO;
import br.com.caelum.livraria.modelo.Autor;
import br.com.caelum.livraria.modelo.Livro;

@ManagedBean
@ViewScoped
public class LivroBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private Livro livro = new Livro();
    private Integer autorId;

    public void setAutorId(Integer autorId) {
        this.autorId = autorId;
    }

    public Integer getAutorId() {
        return autorId;
    }

    public Livro getLivro() {
        return livro;
    }

    public List<Livro> getLivros() {
        return new DAO<Livro>(Livro.class).listaTodos();
    }

    public List<Autor> getAutores() {
        return new DAO<Autor>(Autor.class).listaTodos();
    }

    public List<Autor> getAutoresDoLivro() {
        return this.livro.getAutores();
    }

    public void gravarAutor() {
        Autor autor = new DAO<Autor>(Autor.class).buscaPorId(this.autorId);
        this.livro.adicionaAutor(autor);
    }

    public void gravar() {
        System.out.println("Gravando livro " + this.livro.getTitulo());

        if (livro.getAutores().isEmpty()) {
            throw new RuntimeException("Livro deve ter pelo menos um Autor.");
        }

        new DAO<Livro>(Livro.class).adiciona(this.livro);

        this.livro = new Livro();
    }

}

Desde já agradeço qualquer ajuda =)

2 respostas

Oi Isaac, tem mais alguma mensagem de erro além dessa "javax.el.ELException: /livro.xhtml @36,59 value="#{livroBean.autores}": Error reading [autores] on type [br.com.caelum.livraria.bean.LivroBean]" ???

Olá Otávio, obrigado pela atenção :) Abaixo segue o erro, não era esse pois no front está aparecendo esse aqui

Caused by: java.lang.ExceptionInInitializerError
    at br.com.caelum.livraria.dao.DAO.listaTodos(DAO.java:55)
    at br.com.caelum.livraria.bean.LivroBean.getAutores(LivroBean.java:39)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:94)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:190)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    ... 43 more

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software