Datatable of primefaces does not update selection variable

1

Based on the first example of Datatable Selection where a variable (% with% ) I get the selected value when I click on a linhaSelec I decided to mount my implementation but the variable that would have the value of the selected line is null for the Dialog I wanted to use to change those values.

Stack trace: <p:commandButton/>

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>
    
asked by anonymous 25.04.2018 / 20:59

2 answers

1

You are not initializing the linhaSelec attribute. So the value of it is null and then the Target Unreachable (sometimes called NullPointerException of JSF ) is released.

Initialize the attribute before using it. For example:

Builder Initialization :

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

Initialization by @PostConstruct :

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

Dependency injection (CDI) :

@Inject
private Comercio linhaSelec;
    
26.04.2018 / 18:50
0

Try to make the selection through the datatable itself rather than 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 Commerce class that will be used in rowKey (create tmb equals and hashcode).

    
30.04.2018 / 19:22