Target Unreachable, 'reply' returned null - Selectoneradio

Asked

Viewed 324 times

1

I’m not getting the value of mine selectOneRadio, comes as null:

<p:dataTable id="exibePerguntas" var="questao" paginator="true"
                rowsPerPageTemplate="2,3,5,10,12" paginatorPosition="bottom"
                value="#{responderSimuladoBean.simulado[0].questoes}">
                <p:column headerText="Perguntas">
                    <br></br>
                    <h:outputText escape="false" value="#{questao.pergunta}"  />

                <p:selectOneRadio id="resposta" style="width:25%"
                    value="#{responderSimuladoBean.resposta.respostaUsuario}">
                    <f:selectItem itemLabel="A" itemValue="A" />
                    <f:selectItem itemLabel="B" itemValue="B" />
                    <f:selectItem itemLabel="C" itemValue="C" />
                    <f:selectItem itemLabel="D" itemValue="D" />
                </p:selectOneRadio>

                    <h:outputText value="#{responderSimuladoBean.resposta.resultado}" />
                </p:column>
            </p:dataTable>

            <p:commandButton process="@(#exibePerguntas)" id="enviarSimulado"
                value="Enviar Simulado" action="#{responderSimuladoBean.corrigirSimulado}"
                icon="ui-icon-search" iconPos="right" update=":frmCadastroSimulado">
            </p:commandButton>

The value that is coming null is the value="#{responderSimuladoBean.resposta.respostaUsuario}">

Follow the error:

GRAVE: javax.el.Propertynotfoundexception: /Simulado.xhtml @25,64 value="#{replySimuladoBean.resposta.answerUsuario}": Target Unreachable, 'reply' returned null

But so far I haven’t been able to identify the problem.

Follow my bean:

@Named
@ViewScoped
public class ResponderSimuladoBean implements Serializable {

    private static final long serialVersionUID = 3071538719985705117L;

    @Inject
    private SimuladoDAO simuladoDAO;

    @Inject
    private RespostaService respostaService;

    private Resposta resposta;
    private Usuario usuario = new Usuario();
    private List<Simulado> simulado;
    private List<Resposta> respostas = new ArrayList<Resposta>();

    @PostConstruct
    private void init() {

        simulado = this.simuladoDAO.buscaPorTodosSimulados();
    }

    @Transactional
    public void corrigirSimulado() throws Exception {
        respostas.clear();

        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
        Long codigoUsuario = (Long) session.getAttribute("identificaUsuario");
        usuario.setCodigo(codigoUsuario);

        for (Simulado s : simulado) {
            for (Questao q : s.getQuestoes()) {
                this.limpar();
                resposta.setUsuario(usuario);
                resposta.setQuestao(q);
                resposta.setRespostaCorreta(q.getRespostaPadraoPerguntaItem());
                respostas.add(resposta);
            }
        }

        this.respostaService.salvar(respostas);
    }

    private void limpar() {
        this.resposta = new Resposta();
    }

    public List<Simulado> getSimulado() {
        return simulado;
    }

    public List<Resposta> getRespostas() {
        return respostas;
    }

    public Resposta getResposta() {
        return resposta;
    }

    public void setResposta(Resposta resposta) {
        this.resposta = resposta;
    }

}

1 answer

1


The mistake Target Unreachable, 'resposta' returned null means someone on the way back null, so JSF cannot recover the value of the property, i.e., or the responderSimuladoBean is null (I don’t think it’s likely) or responderSimuladoBean.resposta is (I think the likely).

However, I believe you are using the wrong path in selectOneRadio, because there’s only one answer in your bean.

I believe you should change it to something like:

@Named
@ViewScoped
public class ResponderSimuladoBean implements Serializable {

    private static final long serialVersionUID = 3071538719985705117L;

    @Inject
    private SimuladoDAO simuladoDAO;

    @Inject
    private RespostaService respostaService;

    // Removi um aqui
    private Usuario usuario = new Usuario();
    private List<Simulado> simulado;
    private List<Resposta> respostas = new ArrayList<Resposta>();

    @PostConstruct
    private void init() {
        simulado = this.simuladoDAO.buscaPorTodosSimulados();

        // Alterei para aqui
        for (Simulado s : simulado) {
            for (Questao q : s.getQuestoes()) {
                this.limpar();
                resposta.setUsuario(usuario);
                resposta.setQuestao(q);
                resposta.setRespostaCorreta(q.getRespostaPadraoPerguntaItem());
                respostas.add(resposta);
            }
        }
    }

    @Transactional
    public void corrigirSimulado() throws Exception {
        respostas.clear();

        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
        Long codigoUsuario = (Long) session.getAttribute("identificaUsuario");
        usuario.setCodigo(codigoUsuario);

        this.respostaService.salvar(respostas);
    }

    // código igual daqui para baixo

}

After that, you should change your JSF mapping to something like:

        <p:dataTable id="exibePerguntas" var="resposta" paginator="true"
            rowsPerPageTemplate="2,3,5,10,12" paginatorPosition="bottom"
            value="#{responderSimuladoBean.respostas}">
            <p:column headerText="Perguntas">
                <br></br>
                <h:outputText escape="false" value="#{questao.pergunta}"  />

            <p:selectOneRadio id="resposta" style="width:25%"
                value="#{resposta.respostaUsuario}">
                <f:selectItem itemLabel="A" itemValue="A" />
                <f:selectItem itemLabel="B" itemValue="B" />
                <f:selectItem itemLabel="C" itemValue="C" />
                <f:selectItem itemLabel="D" itemValue="D" />
            </p:selectOneRadio>

                <h:outputText value="#{resposta.resultado}" />
            </p:column>
        </p:dataTable>

        <p:commandButton process="@(#exibePerguntas)" id="enviarSimulado"
            value="Enviar Simulado" action="#{responderSimuladoBean.corrigirSimulado}"
            icon="ui-icon-search" iconPos="right" update=":frmCadastroSimulado">
        </p:commandButton>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.