Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding filterBy to p:dataTable causes java.lang.NullPointerException at javax.faces.component.StateHolderSaver.<init> on postback

I'm trying to develop a datatable with lazy loading. Unfiltered and filtered records are correctly loaded from the persistence context through the use of UserService (i.e. a simple DAO). I'm 100% sure the problem is not related to the DAO being used because it has been successfully tested and deployed in other packages.

The error comes out when I try to insert a filter: the collection of filtered items is correctly loaded on the page but it looks like at the end of the process something goes wrong and a NPE is thrown. After that, the table no longer responds to any command.

Thanks for any help!

In the following, the configuration, the view and the controller being used.

Configuration

  • Wildfly 8.2.0
  • JDK 1.8.0_25
  • PrimeFaces 5.1

View (XHTML page)

<?xml version="1.0" encoding="UTF-8" ?>
<!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:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">

    <ui:composition template="/template/template.xhtml">

        <ui:define name="body">
            <h:form id="UserListForm">
                <p:panel header="#{bundle.ListUserTitle}">
                    <p:dataTable id="UserTable" value="#{userController.users}" var="user" lazy="true" rows="25"
                                 paginator="true" rowKey="#{user.id}" selectionMode="single" selection="#{userController.selected}"
                                 paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}" >

                        <p:column headerText="#{bundle.User_id}" filterBy="#{user.id}">
                           <h:outputText value="#{user.id}"/>
                        </p:column>
                        <p:column headerText="#{bundle.User_nickname}" filterBy="#{user.nickname}">
                           <h:outputText value="#{user.nickname}"/>
                        </p:column>

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

        </ui:define>
    </ui:composition>

</html>

Controller

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;

@Named("userController")
@SessionScoped
@Transactional(Transactional.TxType.REQUIRED)
public class UserController implements Serializable {

    @Inject
    private UserService userService;

    private LazyUserDataModel users;
    private User selected;

    @PostConstruct
    public void init() {
        users = new LazyUserDataModel(userService);
    }

    public User getSelected() {
        return selected;
    }

    public void setSelected(User selected) {
        this.selected = selected;
    }

    public LazyUserDataModel getUsers() {
        return users;
    }

    public void setUsers(LazyUserDataModel users) {
        this.users = users;
    }

}

Lazy Data Model

import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

public class LazyUserDataModel extends LazyDataModel<User> {

    private List<User> data;
    private final UserService userService;

    public LazyUserDataModel(UserService userService) {
        this.userService = userService;
    }

    @Override
    public User getRowData(String rowKey) {
        Long id = Long.valueOf(rowKey);
        for (User u : data) {
            if (u.getId().equals(id))
                return u;
        }
        return null;
    }

    @Override
    public Object getRowKey(User u) {
        return u.getId();
    }

    @Override
    public List<User> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters) {
        UserFilterCorrector.correct(filters);
        UserFilter filter = new UserFilter(filters, first, pageSize);
        FilteredDataModel<User> userDataModel = userService.findFilteredList(filter);
        data = userDataModel.getData();
        this.setRowCount(userDataModel.getCount().intValue());
        return data;
    }
}

Exception

11:07:46,348 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (default task-97) Error Rendering View[/user/List.xhtml]: java.lang.NullPointerException
 at javax.faces.component.StateHolderSaver.<init>(StateHolderSaver.java:96) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at javax.faces.component.UIComponentBase.saveAttachedState(UIComponentBase.java:1746) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at javax.faces.component.ComponentStateHelper.saveMap(ComponentStateHelper.java:378) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at javax.faces.component.ComponentStateHelper.saveState(ComponentStateHelper.java:256) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at javax.faces.component.UIComponentBase.saveState(UIComponentBase.java:1552) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at javax.faces.component.UIData.saveState(UIData.java:1780) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at org.primefaces.component.datatable.DataTable.saveState(DataTable.java:1520) [primefaces-5.1.jar:5.1]
 at com.sun.faces.application.view.FaceletPartialStateManagementStrategy$3.visit(FaceletPartialStateManagementStrategy.java:482) [jsf-impl-2.2.8-jbossorg-1.jar:]
 at com.sun.faces.component.visit.FullVisitContext.invokeVisitCallback(FullVisitContext.java:151) [jsf-impl-2.2.8-jbossorg-1.jar:]
 at org.primefaces.component.api.UIData.visitTree(UIData.java:692) [primefaces-5.1.jar:5.1]
 at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at javax.faces.component.UIForm.visitTree(UIForm.java:371) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at com.sun.faces.application.view.FaceletPartialStateManagementStrategy.saveView(FaceletPartialStateManagementStrategy.java:472) [jsf-impl-2.2.8-jbossorg-1.jar:]
 at com.sun.faces.application.StateManagerImpl.saveView(StateManagerImpl.java:89) [jsf-impl-2.2.8-jbossorg-1.jar:]
 at javax.faces.application.StateManager.getViewState(StateManager.java:593) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at com.sun.faces.context.PartialViewContextImpl.renderState(PartialViewContextImpl.java:483) [jsf-impl-2.2.8-jbossorg-1.jar:]
 at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:325) [jsf-impl-2.2.8-jbossorg-1.jar:]
 at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60) [primefaces-5.1.jar:5.1]
 at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:1004) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:430) [jsf-impl-2.2.8-jbossorg-1.jar:]
 at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133) [jsf-impl-2.2.8-jbossorg-1.jar:]
 at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) [jsf-impl-2.2.8-jbossorg-1.jar:]
 at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.2.8-jbossorg-1.jar:]
 at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) [jsf-impl-2.2.8-jbossorg-1.jar:]
 at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
 at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
 at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
 at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
 at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
 at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:247) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:76) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:166) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.server.Connectors.executeRootHandler(Connectors.java:197) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
 at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:759) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_25]
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_25]
 at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_25]
like image 532
a.periz Avatar asked Jan 09 '15 10:01

a.periz


People also ask

How to fix exception in thread main java lang NullPointerException?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

How to avoid NullPointerException in java array?

Answer: Some of the best practices to avoid NullPointerException are: Use equals() and equalsIgnoreCase() method with String literal instead of using it on the unknown object that can be null. Use valueOf() instead of toString() ; and both return the same result. Use Java annotation @NotNull and @Nullable.

How to find cause of NullPointerException?

lang. NullPointerException. Detecting NullPointerException is very easy, just look at the exception trace and it will show you the class name and line number of the exception. Then look at the code and see what could be null causing the NullPointerException.

How to raise NullPointerException in java?

NullPointerException is a runtime exception and it is thrown when the application try to use an object reference which has a null value. For example, using a method on a null reference.


2 Answers

I solved mine by removing filter value that had null and replacing it with an empty object. You can put breakpoint in the jsf-api/2.2.8/jsf-api-2.2.8-sources.jar!/javax/faces/component/UIComponentBase.java:1746 and see which value in the filter map is causing the error and then replace it by empty object or remove it. I hope this helps.

like image 76
Bran Avatar answered Oct 03 '22 05:10

Bran


This must be a bug in the jsf api implementation. As you described in your answer @bcavlin there´s not sufficient null-checks in the source code to store a Map that has any key mapped to a null object. I filed a JIRA issue on this and described it in more dept there, see JAVASERVERFACES-4088 (old java.net issue) resp. JAVASERVERFACES-4092.

like image 23
Mattias Olofsson Avatar answered Oct 03 '22 07:10

Mattias Olofsson