Datatable of the first faces does not update selection variable

Asked

Viewed 364 times

1

Based on the first example of the site Datatable Selection where a variable(linhaSelec) receives the value selected by clicking on a <p:commandButton/> I decided to mount my implementation but the variable that would have the value of the selected line comes null for the dialog I intended to use to change these values.

Stack trace: Caused by: javax.el.PropertyNotFoundException: Target Unreachable, [linhaSelec] returned null at org.apache.el.parser.AstValue.getTarget(AstValue.java:124) at org.apache.el.parser.AstValue.getType(AstValue.java:58) at org.apache.el.ValueExpressionImpl.getType(ValueExpressionImpl.java:168) at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:98) ... 41 more

Managedbean:

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.view.ViewScoped;

import com.company.sistema.model.Comercio;
import com.company.sistema.model.InfoAtividade;

@ManagedBean
@ViewScoped
public class ComercioBean implements Serializable {

    private static final long serialVersionUID = 9095360855948557201L;
    private InfoAtividade info;
    private List<Comercio> tabela;
    private Comercio linhaSelec;

    public ComercioBean() {
        info = new InfoAtividade();
        info.setTipo("comercio");

        tabela = new ArrayList<>();
        tabela.add(new Comercio());
        tabela.add(new Comercio());
        tabela.add(new Comercio());
        tabela.add(new Comercio());
        tabela.add(new Comercio());
        tabela.add(new Comercio());

        tabela.forEach(var -> var.setInfoC(info));
        info.setTabelaC(tabela);
    }

    public void calculaAlqt(ActionEvent action) {

        linhaSelec.setAliquota(linhaSelec.getIrpj()
                .add(linhaSelec.getCsll())
                .add(linhaSelec.getCofins())
                .add(linhaSelec.getPispasep())
                .add(linhaSelec.getCpp())
                .add(linhaSelec.getIcms()));
        addMessage(linhaSelec.getIrpj().toString());

    }

    private void addMessage(String summary) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void calculaReferencias(ActionEvent action) {
        linhaSelec.setIrpj(linhaSelec.getAliquota().multiply(linhaSelec.getIrpj()).divide(new BigDecimal("100.0")));
        linhaSelec.setCsll(linhaSelec.getAliquota().multiply(linhaSelec.getCsll()).divide(new BigDecimal("100.0")));
        linhaSelec.setCofins(linhaSelec.getAliquota().multiply(linhaSelec.getCofins()).divide(new BigDecimal("100.0")));
        linhaSelec.setPispasep(linhaSelec.getAliquota().multiply(linhaSelec.getPispasep()).divide(new BigDecimal("100.0")));
        linhaSelec.setCpp(linhaSelec.getAliquota().multiply(linhaSelec.getCpp()).divide(new BigDecimal("100.0")));
        linhaSelec.setIcms(linhaSelec.getAliquota().multiply(linhaSelec.getIcms()).divide(new BigDecimal("100.0")));
    }

    public void salvarTabela(ActionEvent action) {

    }

    public void buscaTabela(ActionEvent action) {

    }

    public void deletaTabela(ActionEvent action) {

    }

    public Comercio getLinhaSelec() {
        return linhaSelec;
    }

    public void setLinhaSelec(Comercio linhaSelec) {
        this.linhaSelec = linhaSelec;
    }

    public InfoAtividade getInfo() {
        return info;
    }

    public void setInfo(InfoAtividade info) {
        this.info = info;
    }

    public List<Comercio> getTabela() {
        return tabela;
    }

    public void setTabela(List<Comercio> tabela) {
        this.tabela = tabela;
    }

}

And the xhtml:

<!DOCTYPE html>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    template="/WEB-INF/templates/Layout.xhtml">

    <ui:define name="content">
        <h1 class="aw-page-title">Gerenciamento de Comércio</h1>
        <h:form id="frmComercio">

            <h:panelGrid columns="2" cellpadding="5">
                <p:outputLabel for="nome" value="Nome:" />
                <p:inputText id="nome" required="true"
                    value="#{comercioBean.info.nome}" />

                <p:outputLabel for="descricao" value="Descrição:" />
                <p:inputTextarea id="descricao" required="true"
                    value="#{comercioBean.info.descricao}" />

            </h:panelGrid>
            <br />
            <p:separator />
            <br />

            <p:dataTable id="tablComercio" var="comercio"
                value="#{comercioBean.tabela}" style="margin-bottom:20px">

                <f:facet name="header">
                    Nova Tabela de Comércio 
                </f:facet>

                <p:column headerText="Receita Bruta Máx.">
                    <h:outputText value="#{comercio.receitaBrtMax}" />
                </p:column>

                <p:column headerText="Receita Bruta Min.">
                    <h:outputText value="#{comercio.receitaBrtMin}" />
                </p:column>

                <p:column headerText="Alíquota">
                    <h:outputText value="#{comercio.aliquota}" />
                </p:column>

                <p:column headerText="IRPJ">
                    <h:outputText value="#{comercio.irpj}" />
                </p:column>

                <p:column headerText="CSLL">
                    <h:outputText value="#{comercio.csll}" />
                </p:column>

                <p:column headerText="Cofins">
                    <h:outputText value="#{comercio.cofins}" />
                </p:column>

                <p:column headerText="PIS/PASEP">
                    <h:outputText value="#{comercio.pispasep}" />
                </p:column>

                <p:column headerText="Cpp">
                    <h:outputText value="#{comercio.cpp}" />
                </p:column>

                <p:column headerText="Icms">
                    <h:outputText value="#{comercio.icms}" />
                </p:column>

                <p:column headerText="Valor a Deduzir">
                    <h:outputText value="#{comercio.valorDeduzir}" />
                </p:column>

                <p:column style="width:32px;text-align: center">
                    <p:commandButton update=":frmComercio:comercioGrd"
                        oncomplete="PF('comercioDialog').show()" icon="ui-icon-search"
                        title="Editar">
                        <f:setPropertyActionListener value="#{comercio}"
                            target="#{comercioBean.linhaSelec}" />
                    </p:commandButton>
                </p:column>

            </p:dataTable>

            <p:toolbar>

                <f:facet name="left">
                    <p:growl id="growl" sticky="true" />

                    <p:commandButton id="btnSalvar" value="Salvar" icon="ui-icon-disk"
                        actionListener="#{comercioBean.salvarTabela}" />
                    <p:commandButton id="btnBusca" value="Buscar"
                        icon="fa fa-fw fa-search"
                        actionListener="#{comercioBean.buscaTabela}" />
                </f:facet>

                <f:facet name="right">
                    <p:commandButton id="btnDeletar" value="Deletar"
                        icon="ui-icon-trash" actionListener="#{comercioBean.deletaTabela}" />
                </f:facet>

            </p:toolbar>

            <p:dialog header="Linha" widgetVar="comercioDialog" modal="true"
                showEffect="fade" hideEffect="fade" resizable="false">
                    <p:outputPanel id="comercioGrd">
                        <p:panelGrid columns="2">
                            <p:outputLabel for="fldReceitaBrtMx" value="Receita Bruta Máx." />
                            <p:inputText value="#{comercioBean.linhaSelec.receitaBrtMax}"
                                style="width:100%" id="fldReceitaBrtMx" />
                            <p:outputLabel for="fldReceitaBrtMn" value="Receita Bruta Min." />
                            <p:inputText value="#{comercioBean.linhaSelec.receitaBrtMin}"
                                style="width:100%" id="fldReceitaBrtMn" />
                            <p:outputLabel for="fldAlqt" value="Alíquota" />
                            <p:inputText value="#{comercioBean.linhaSelec.aliquota}"
                                style="width:100%" id="fldAlqt" />
                            <p:outputLabel for="fldIrpj" value="IRPJ" />
                            <p:inputText value="#{comercioBean.linhaSelec.irpj}"
                                style="width:100%" id="fldIrpj" />
                            <p:outputLabel for="fldCsll" value="CSLL" />
                            <p:inputText value="#{comercioBean.linhaSelec.csll}"
                                style="width:100%" id="fldCsll" />
                            <p:outputLabel for="fldCofins" value="Cofins" />
                            <p:inputText value="#{comercioBean.linhaSelec.cofins}"
                                style="width:100%" id="fldCofins" />
                            <p:outputLabel for="fldPispasep" value="PIS/PASEP" />
                            <p:inputText value="#{comercioBean.linhaSelec.pispasep}"
                                style="width:100%" id="fldPispasep" />
                            <p:outputLabel for="fldCpp" value="Cpp" />
                            <p:inputText value="#{comercioBean.linhaSelec.cpp}"
                                style="width:100%" id="fldCpp" />
                            <p:outputLabel for="fldIcms" value="Icms" />
                            <p:inputText value="#{comercioBean.linhaSelec.icms}"
                                style="width:100%" id="fldIcms" />
                            <p:outputLabel for="fldVlDeduzir" value="Valor a Deduzir" />
                            <p:inputText value="#{comercioBean.linhaSelec.valorDeduzir}"
                                style="width:100%" id="fldVlDeduzir" />

                            <p:commandButton value="Calcular Aliquota" id="btnAlqt"
                                actionListener="#{comercioBean.calculaAlqt}" />

                            <p:commandButton value="Calcular Referencia" id="btnReferencias"
                                actionListener="#{comercioBean.calculaReferencias}"
                                />

                        </p:panelGrid>

                    </p:outputPanel>
            </p:dialog>

        </h:form>

    </ui:define>

</ui:composition>

Follow the template code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<h:head>
    <f:facet name="first">
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    </f:facet>

    <title>NomeAPP</title>

    <h:outputStylesheet library="company" name="styles/custom.css" />
    <h:outputStylesheet library="company" name="styles/layout.css" />
    <h:outputStylesheet library="company" name="styles/components.css" />

    <h:outputScript target="body" library="primefaces"
        name="jquery/jquery.js" />
    <h:outputScript target="body" library="company"
        name="javascripts/app.js" />
</h:head>

<h:body>

    <header class="aw-topbar">
        <h:graphicImage library="company" name="images/logo.png" />

        <a href="javascript:void(0);" class="aw-toggle  js-toggle"><i
            class="fa  fa-bars"></i></a>
    </header>

    <aside class="aw-sidebar  js-sidebar">

        <nav class="aw-menu">
            <h:form>
                <p:menu>
                    <p:submenu label="Principal">
                        <p:menuitem value="Início" icon="fa fa-fw fa-home" outcome="/faces/Index.xhtml" />
                    </p:submenu>
                    <p:submenu label="Funcionais">
                        <p:menuitem value="Option1" icon="fa fa-fw fa-bar-chart" />
                        <p:menuitem value="Option2" icon="fa fa-fw fa-copyright"  />
                    </p:submenu>
                    <p:submenu label="Atividades">
                        <p:menuitem value="Comércio" icon="fa fa-fw fa-shopping-cart" outcome="/faces/atividades/Comercio.xhtml" />
                        <p:menuitem value="Option2" icon="fa fa-fw fa-building" />
                        <p:menuitem value="Option 3" icon="fa fa-fw fa-cubes" />
                        <p:menuitem value="Option 4" icon="fa fa-fw fa-cubes" />
                        <p:menuitem value="Option5" icon="fa fa-fw fa-cubes" />
                    </p:submenu>
                    <p:submenu label="Conta">
                        <p:menuitem value="Sobre" icon="fa fa-fw fa-info-circle" outcome="/faces/Sobre.xhtml" />
                    </p:submenu>
                </p:menu>
            </h:form>
        </nav>
    </aside>

    <section class="aw-content  js-content">
        <ui:insert name="content" />
    </section>

</h:body>

</html>
  • See in the browser console if there is no error message.

  • You can post your template (Layout.xhtml)?

  • Nothing appears on the console, about the template I’ll see here but I think the question will exceed the size limit... Sorry it took me so long to answer

2 answers

1

You are not initiating the attribute linhaSelec.
That way his value is null and then the Target Unreachable (sometimes called NullPointerException of the JSF) is launched.

Initialize the attribute before using it. For example:

Initialization by the constructor:

public ComercioBean() {
    this.linhaSelec = new Comercio();
}

Initialization by @PostConstruct:

@PostConstruct
public void init() {
    this.linhaSelec = new Comercio();
}

Injection of dependencies (CDI):

@Inject
private Comercio linhaSelec;
  • Thank you for answering Igor, but the idea is that this reference points to the instance selected in the datatable, as well as in the referenced example of the first faces itself the variable that plays the 'lineSelec' has no instance, I did as you said and my dialog showed a specific value that I put to the test and not the value contained in the row represented by the table

  • @William.Andrade got it. You make one update=":frmComercio:frmDlg:comercioGrd" but frmDlg does not exist in the code you posted.. Just for testing purposes, remove this update and debug to see if at the click of the button the value of linhaSelec is being sent (puts a brakepoint on his Tter)

  • Opa had already fixed this error that generated another Excpetion, but failed to update the code in the post I will update right now. The javax.el.Propertynotfoundexception error still persisted after I had previously fixed it.

  • @William.Andrade the class Comercio has a constructor without arguments?

  • Has no arguments yes

0

Try making the selection through the datatable itself instead of the button. This way:

    <h:form id="frmComercio">
        <p:dataTable id="tablComercio" var="comercio"
            value="#{comercioBean.tabela}" style="margin-bottom:20px"
            selectionMode="single" selection="#{comercioBean.linhaSelec}" rowKey="#{comercio.id}">

            <p:ajax event="rowSelect" update=":frmComercio:comercioGrd" oncomplete="PF('comercioDialog').show()" />

            <f:facet name="header">
                Nova Tabela de Comércio 
            </f:facet>

            <p:column headerText="Valor a Deduzir">
                <h:outputText value="#{comercio.valorDeduzir}" />
            </p:column>

        </p:dataTable>

        <p:dialog header="Linha" widgetVar="comercioDialog" >
                <p:outputPanel id="comercioGrd">
                    <p:panelGrid columns="2">
                        <p:outputLabel for="fldVlDeduzir" value="Valor a Deduzir" />
                        <p:inputText value="#{comercioBean.linhaSelec.valorDeduzir}"
                            style="width:100%" id="fldVlDeduzir" />

                    </p:panelGrid>
                    <p:commandButton value="ok" update=":frmComercio:tablComercio" oncomplete="PF('comercioDialog').hide()" />
                </p:outputPanel>
        </p:dialog>

    </h:form>

Note: You need to create a key attribute in your Trade class that will be used in rowKey (create tmb the equals and hashcode).

  • Guy did it the way his example and it worked, after I hit enter the value of the input is reflected in the datatable, I tried it using the second example of the site of the primefaces (Single Selection) only that as a difference by pressing enter the value entered in the input returns to 0.0 and the dialog does not close, you have some idea of why?

  • The values were only reflected with a column... with more than one the values were only stored in the dialogs... ie the table gets the values zeroed, I tried to force the bar and put <p:ajax event="close" update=":frmComercio:tablComercio"/> but it remains untouched...

  • I only put a column to illustrate, you are who determine how many and which should appear.

  • See the OK button in the edition I made to update the datatable.

  • I put here and did not update the table...

Browser other questions tagged

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