0

I'm using primefaces for my webapp and I'm having some problems with my beans. I have a page with two tables, each table has its own bean with an attribute for the selected option in the table, the thing is that when I select a value from the FIRST table and i try to change it, the server crash and tells me that the selected attribute of the SECOND table is null. I don't know why is using the selected attribute of the second table if I'm selecting a row of the first table.

this is my html page:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<h:head>
    <title> BATCH PARAMETRIZATION</title>

</h:head>
<h:body>

<h:form id="form">
    <p:panel>       

        <p:dataTable 
                id="singleDT" 
                var="Params" 
                value="#{tableMB.precios}" 
                rendered="true"
                selectionMode="single" 
                selection="#{tableMB.selectedParam}" 
                rowKey="#{Params.id}"
                style="margin-top:10">

                <f:facet name="header">
                    Parametrizacion
                </f:facet>
                <p:column headerText="Valor">
                    <h:outputText value="$#{Params.valor}" />
                </p:column>
                <p:column headerText="Porcentaje">
                    <h:outputText value="#{Params.porcentaje*100}%" />
                </p:column>
                <f:facet name="footer" >
                    <div align="left"> 
                        <p:commandButton process="singleDT" update=":form:detalleParametro" icon="ui-icon-search" 
                            value="Modificar" oncomplete="PF('modificarParam').show()" 
                        />
                        <p:commandButton process="singleDT" update=":form:detalleParametro" icon="ui-icon-search" 
                            value="Agregar" oncomplete="PF('agregarParam').show()" 
                        />
                    </div>
                </f:facet>
            </p:dataTable>

            <p:dialog header="Parametro" widgetVar="modificarParam" modal="true" showEffect="fade" hideEffect="fade" resizable="false">
                <p:outputPanel id="detalleParametro" style="text-align:center;">
                <p:panelGrid  columns="2" rendered="#{not empty tableMB.selectedParam}" columnClasses="label,value">
                    <h:outputText value="Id:" />
                    <h:outputText value="#{tableMB.selectedParam.id}" />

                    <h:outputText value="Valor" />
                    <p:inputText value="#{tableMB.selectedParam.valor}" required="true">
                    </p:inputText>

                    <h:outputText value="Porcentaje:" />
                    <p:inputText value="#{tableMB.selectedParam.porcentaje}" required="true" />

                </p:panelGrid>
                    <p:commandButton update=":form:singleDT" value="Aceptar" action="#{tableMB.modificarParametros()}"
                                oncomplete="PF('modificarParam').hide()" style="margin-top:10" 
                    />
                </p:outputPanel>
            </p:dialog>

            <p:dialog header="Parametro" widgetVar="agregarParam" modal="true" showEffect="fade" hideEffect="fade" resizable="false">
                <p:outputPanel id="detalleParametro2" style="text-align:center;">
                <p:panelGrid  columns="2" columnClasses="label,value">
                    <h:outputText value="Valor" />
                    <p:inputText value="#{tableMB.nuevoParam.valor}" required="true">
                    </p:inputText>

                    <h:outputText value="Porcentaje:" />
                    <p:inputText value="#{tableMB.nuevoParam.porcentaje}" required="true" />

                </p:panelGrid>
                    <p:commandButton update=":form:singleDT" value="Aceptar" action="#{tableMB.agregarParametros()}"
                                oncomplete="PF('agregarParam').hide()" style="margin-top:10" 
                    />
                </p:outputPanel>
            </p:dialog>

    </p:panel>

    <p:panel>

            <p:dataTable 
                id="premiosDT" 
                var="Premios" 
                value="#{tablePremiosMB.premios}" 
                rendered="true"
                selectionMode="single" 
                selection="#{tablePremiosMB.selectedPremio}" 
                rowKey="#{Premios.id}"
                style="margin-top:10">


                <p:column headerText="Premio">
                    <h:outputText value="#{Premios.premio}" />
                </p:column>
                <p:column headerText="Porcentaje">
                    <h:outputText value="#{Premios.porcentaje*100}%" />
                </p:column>
                <f:facet name="footer" >
                    <div align="left"> 
                        <p:commandButton process="premiosDT" update=":form:detallePremio" icon="ui-icon-search" 
                            value="Modificar" oncomplete="PF('modificarPremio').show()" 
                        />
                        <p:commandButton process="premiosDT" update=":form:detallePremio2" icon="ui-icon-search" 
                            value="Agregar" oncomplete="PF('agregarPremio').show()" 
                        />
                    </div>
                </f:facet>
            </p:dataTable>

            <p:dialog header="Premio" widgetVar="modificarPremio" modal="true" showEffect="fade" hideEffect="fade" resizable="false">
                <p:outputPanel id="detallePremio" style="text-align:center;">
                <p:panelGrid  columns="2" rendered="true" columnClasses="label,value">
                    <h:outputText value="Id:" />
                    <h:outputText value="#{tablePremiosMB.selectedPremio.id}" />

                    <h:outputText value="Descripcion" />
                    <p:inputText value="#{tablePremiosMB.selectedPremio.premio}" required="true">
                    </p:inputText>

                    <h:outputText value="Porcentaje:" />
                    <p:inputText value="#{tablePremiosMB.selectedPremio.porcentaje}" required="true" />

                </p:panelGrid>
                    <p:commandButton update=":form:premiosDT" value="Aceptar" action="#{tablePremiosMB.modificarPremio()}"
                                oncomplete="PF('modificarPremio').hide()" style="margin-top:10" 
                    />
                </p:outputPanel>
            </p:dialog>

            <p:dialog header="Premio" widgetVar="agregarPremio" modal="true" showEffect="fade" hideEffect="fade" resizable="false">
                <p:outputPanel id="detallePremio2" style="text-align:center;">
                <p:panelGrid  columns="2" columnClasses="label,value">
                    <h:outputText value="Descripcion" />
                    <p:inputText value="#{tablePremiosMB.nuevoPremio.premio}" required="true">
                    </p:inputText>

                    <h:outputText value="Porcentaje:" />
                    <p:inputText value="#{tablePremiosMB.nuevoPremio.porcentaje}" required="true" />

                </p:panelGrid>
                    <p:commandButton update=":form:premiosDT" value="Aceptar" action="#{tablePremiosMB.agregarPremio()}"
                                oncomplete="PF('agregarPremio').hide()" style="margin-top:10" 
                    />
                </p:outputPanel>
            </p:dialog>
    </p:panel>  
</h:form>

</h:body>


</html>

this is the bean for the first table:

package ar.com.nortid.mb;

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

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

@ManagedBean(name="tableMB")
@ViewScoped
public class TableMB implements Serializable {

    /**
     * 
     */
    private List<Params> precios = new ArrayList<Params>();
    private Params selectedParam;
    private Params nuevoParam = new Params();
    private String expirationDate;

    public TableMB() {
        precios.add(new Params(new Long(1), 2.0, 0.3));
        precios.add(new Params(new Long(2), 5.0, 0.2));
        precios.add(new Params(new Long(3), 10.0, 0.1));
    }

    public List<Params> getPrecios() {
        return precios;
    }

    public void setPrecios(List<Params> precios) {
        this.precios = precios;
    }

    public Params getSelectedParam() {
        return selectedParam;
    }

    public void setSelectedParam(Params selectedParam) {
        this.selectedParam = selectedParam;
    }   

    public Params getNuevoParam() {
        return nuevoParam;
    }

    public void setNuevoParam(Params nuevoParam) {
        this.nuevoParam = nuevoParam;
    }

    public String getExpirationDate() {
        return expirationDate;
    }

    public void setExpirationDate(String expirationDate) {
        this.expirationDate = expirationDate;
    }

    public void modificarParametros(){

        int index = -1;
        for(int i=0; i<precios.size(); i++)
            if(precios.get(i).getId() == selectedParam.getId())
                index = i;

        if(index >= 0){
            precios.remove(index);
            precios.add(selectedParam);
        }
    }

    public void agregarParametros(){

        nuevoParam.setId(new Long(precios.size()));
        precios.add(nuevoParam);
        nuevoParam = null;
        nuevoParam = new Params();
    }
}



package ar.com.nortid.mb;

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

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

@ManagedBean(name="tablePremiosMB")
@ViewScoped
public class TablePremiosMB implements Serializable {

    /**
     * 
     */
    private List<Premios> premios = new ArrayList<Premios>();
    private Premios selectedPremio;
    private Premios nuevoPremio = new Premios();

    public TablePremiosMB() {
        premios.add(new Premios(new Long(1), "Departamento", 0.001));
        premios.add(new Premios(new Long(2), "Auto", 0.005));
        premios.add(new Premios(new Long(3), "$10000", 0.01));
        premios.add(new Premios(new Long(4), "$100", 0.2));
    }

    public List<Premios> getPremios() {
        return premios;
    }

    public void setPremios(List<Premios> premios) {
        this.premios = premios;
    }

    public Premios getSelectedPremio() {
        return selectedPremio;
    }

    public void setSelectedPremio(Premios selectedPremio) {
        this.selectedPremio = selectedPremio;
    }

    public Premios getNuevoPremio() {
        return nuevoPremio;
    }

    public void setNuevoPremio(Premios nuevoPremio) {
        this.nuevoPremio = nuevoPremio;
    }

    public void modificarPremio(){

        int index = -1;
        for(int i=0; i<premios.size(); i++)
            if(premios.get(i).getId() == selectedPremio.getId())
                index = i;

        if(index >= 0){
            premios.remove(index);
            premios.add(selectedPremio);
        }
    }

    public void agregarPremio(){

        nuevoPremio.setId(new Long(premios.size()));
        premios.add(nuevoPremio);
        nuevoPremio = null;
        nuevoPremio = new Premios();
    }

}

and this is the error that shows up when i try to change a value of the first table:

ADVERTENCIA: /page/BatchParametrization.xhtml @167,83 value="#{tablePremiosMB.selectedPremio.premio}": Objetivo inalcanzable, 'selectedPremio' devolvió nulo
javax.el.PropertyNotFoundException: /page/BatchParametrization.xhtml @167,83 value="#{tablePremiosMB.selectedPremio.premio}": Objetivo inalcanzable, 'selectedPremio' devolvió nulo
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
    at org.primefaces.util.ComponentUtils.getConverter(ComponentUtils.java:132)
    at org.primefaces.renderkit.InputRenderer.getConvertedValue(InputRenderer.java:171)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
    at javax.faces.component.UIInput.validate(UIInput.java:960)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
    at javax.faces.component.UIInput.processValidators(UIInput.java:698)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at org.primefaces.component.dialog.Dialog.processValidators(Dialog.java:387)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at org.primefaces.component.panel.Panel.processValidators(Panel.java:287)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

as you can see, selectedPremio is returning null but selectedPremio is an attribute of the second table and im try to change a row of the first table. I dont know what to do. Any help you can give me will be great.

kolossus
  • 19,953
  • 3
  • 45
  • 94
  • This is not a server crash, just an application error. Describing the problem properly goes a long way to getting the right answers here. You should separate both tables (and their associated components) into separate `h:form`s – kolossus May 24 '15 at 06:17
  • Change your development environment to English. The chance on a successful hit when you copypaste an English exception message into Google or Stack Overflow search is SO MUCH bigger. – BalusC May 24 '15 at 06:19

0 Answers0