Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
846 views
in Technique[技术] by (71.8m points)

jsf - java.lang.NullPointerException at org.primefaces.component.datatable.feature.FilterFeature.filter

I'm trying create a filter to one dataTable. I want that filter works with all keywords contained in datatable.

I am following examples at: http://www.primefaces.org/showcase/ui/data/datatable/filter.xhtml but I can't make it work

The problem is when I enter with any keywords the dataTable is clear showing message "Nenhum equipamento encontrado", if I delete keyword all results doesn't returns and message "Nenhum equipamento encontrado" keeps.

How can I solve this problem ?

I looked up for similar problems and all i find was the possible lack of filterBy="#{}" on the columns, i added then all, but still doesn't work

XHTML

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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"
    xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Equipamentos</title>
</h:head>
<h:body>
    <p:dataTable id="equipamentos" 
                 var="equipamento" 
                 value="#{equipamentoBean.equipamentos}" 
                 widgetVar="equipamentoTable" 
                 filteredValue="#{equipamentoBean.equipamentosFiltrados}"
                 paginator="true" 
                 rows="15" 
                 emptyMessage="Nenhum equipamento encontrado" 
                 resizableColumns="true">

        <f:facet name="header">
        Lista de equipamentos
        <!--            DESLIGAR COLUNAS -->
        <p:commandButton id="toggler" 
                         type="button" 
                         value="Colunas"
                         style="float:right" 
                         icon="ui-icon-calculator" />
            <p:columnToggler datasource="equipamentos" trigger="toggler" />

            <!--            BUSCA -->
            <p:outputPanel>
                <h:outputText value="Busca em todos os campos:"/> 
                <p:inputText id="globalFilter"
                    onkeyup="PF('equipamentoTable').filter()" style="width:250px"
                    placeholder="Palavra-chave para busca" />
            </p:outputPanel>            
        </f:facet>

        <p:column filterBy="#{equipamento.nome}" 
                  sortBy="#{equipamento.nome}" 
                  headerText="Nome">
            <h:outputText value="#{equipamento.nome}"></h:outputText>
        </p:column>

        <p:column filterBy="#{equipamento.equipamentoTipoId}" 
                  sortBy="#{equipamento.equipamentoTipoId}" 
                  headerText="Tipo">
            <h:outputText value="#{equipamento.equipamentoTipoId}"></h:outputText>
        </p:column>

        <p:column filterBy="#{equipamento.equipamentoTipoId.hardwareEquipamentoId}"
                  headerText="Hardware">
            <h:outputText 
                 value="#{equipamento.equipamentoTipoId.hardwareEquipamentoId}">
            </h:outputText>
        </p:column>

        <p:column filterBy="#{equipamento.equipamentoVersaoSoftwareId}" 
                  headerText="Versao Software">
            <h:outputText 
                  value="#{equipamento.equipamentoVersaoSoftwareId}">
            </h:outputText>
        </p:column>

        <p:column filterBy="#{equipamento.equipamentoTipoId.tecnologiaEquipamentoId}"
                  headerText="Tecnologia">
            <h:outputText 
                  value="#{equipamento.equipamentoTipoId.tecnologiaEquipamentoId}"
            </h:outputText>
        </p:column>

        <p:column filterBy="#{equipamento.regiaoId}" headerText="Regional">
            <h:outputText value="#{equipamento.regiaoId}"></h:outputText>       
        </p:column>

        <p:column filterBy="#{equipamento.equipamentoTipoId.areaId}" 
                  headerText="Area">
            <h:outputText 
                 value="#{equipamento.equipamentoTipoId.areaId}">
            </h:outputText>     
        </p:column>

        <p:column filterBy="#{equipamento.ativo}" headerText="Ativo">
            <h:outputText value="#{equipamento.ativo}"></h:outputText>
        </p:column>

    </p:dataTable>
</h:body>
</html>

ManagedBean

package br.com.timbrasil.network.bean;

import java.util.List;



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

import br.com.timbrasil.network.dao.EquipamentoDao;
import br.com.timbrasil.network.modelo.Equipamento;

@ManagedBean
@ViewScoped
public class EquipamentoBean {

    private List<Equipamento> equipamentos;

    private List<Equipamento> equipamentosFiltrados;

    public EquipamentoBean(){
        this.equipamentos = new EquipamentoDao().listaEquipamentos();
        Equipamento eqp = new Equipamento();
    }

    public List<Equipamento> getEquipamentos() {
        return equipamentos;
    }

    public void setEquipamentos(List<Equipamento> equipamentos) {
        this.equipamentos = equipamentos;
    }

    public List<Equipamento> getEquipamentosFiltrados() {
        return equipamentosFiltrados;
    }

    public void setEquipamentosFiltrados(List<Equipamento> equipamentosFiltrados) {
        this.equipamentosFiltrados = equipamentosFiltrados;
    }



}

ERROR

set 24, 2014 7:00:54 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
SEVERE: Error Rendering View[/equipamento.xhtml]
java.lang.NullPointerException
    at org.primefaces.component.datatable.feature.FilterFeature.filter(FilterFeature.java:136)
    at org.primefaces.component.datatable.feature.FilterFeature.encode(FilterFeature.java:105)
    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:77)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:582)
    at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
    at org.primefaces.component.api.UIData.visitTree(UIData.java:692)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:403)
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:322)
    at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
    at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:1004)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:430)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

set 24, 2014 7:00:54 PM com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError
SEVERE: java.lang.NullPointerException
    at org.primefaces.component.datatable.feature.FilterFeature.filter(FilterFeature.java:136)
    at org.primefaces.component.datatable.feature.FilterFeature.encode(FilterFeature.java:105)
    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:77)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:582)
    at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
    at org.primefaces.component.api.UIData.visitTree(UIData.java:692)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:403)
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:322)
    at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
    at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:1004)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:430)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You must put your p:datatable inside h:form like this.

    <h:form id="form-list-equipamentos">
        <p:dataTable id="equipamentos" 
                     var="equipamento" 
                     value="#{equipamentoBean.equipamentos}" 
                     widgetVar="equipamentoTable" 
                     filteredValue="#{equipamentoBean.equipamentosFiltrados}"
                     emptyMessage="Nenhum equipamento encontrado" 
                     resizableColumns="true">

            <f:facet name="header">
                Lista de equipamentos
                <!--            DESLIGAR COLUNAS -->
                <p:commandButton id="toggler" 
                                 type="button" 
                                 value="Colunas"
                                 style="float:right" 
                                 icon="ui-icon-calculator" />
                <p:columnToggler datasource="equipamentos" 
                                 trigger="toggler" />

                <!--            BUSCA -->
                <p:outputPanel>
                    <h:outputText value="Busca em todos os campos:"/> 
                    <p:inputText id="globalFilter"
                                 onkeyup="PF('equipamentoTable').filter()" 
                                 style="width:250px"
                                 placeholder="Palavra-chave para busca" />
                </p:outputPanel>            
            </f:facet>

            <p:column filterBy="#{equipamento.nome}" 
                      sortBy="#{equipamento.nome}" 
                      headerText="Nome" filterMatchMode="contains">
                <h:outputText value="#{equipamento.nome}"></h:outputText>
            </p:column>

        </p:dataTable>
    </h:form>

Also watchout that your equipamentoBean is in proper scope (mine: javax.faces.view.ViewScoped).

OT: I would recommend you to "id" the f:form tag as this gives you the possibility to update it after a "create" form (outside this form!) has added a record:

    <h:form>
    ....
        <p:commandButton
            type="submit"
            value="Add something"
            action="#{someBackingBean.addSomething()}"
            update=":master:form-list-equipamentos:equipamentos"
            />
    </h:form>

The id="master" is in my "master" template where all other views derive from:

    <h:body>
        <pm:page id="master">
        ...
        </pm:page>
    </h:body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...