<p:ajax>侦听器函数中的NullPointerException:Primefaces5

时间:2015-04-26 12:12:31

标签: java jsf nullpointerexception

我有Primefaces&#34; selectCheckboxMenu&#34;并希望动态地在表格中显示所选复选框。

我的xhtml文件内容是:

        <p:selectCheckboxMenu id="formRole"
            value="#{formSecurityBean.roleMulti}" filter="true"
            filterMatchMode="contains" label="Select"
            rendered="#{formSecurityBean.renderFormRoleGrid}">
            <f:selectItems value="#{formSecurityBean.formRoleList}" var="role"
                itemValue="#{role.roleID}" itemLabel="#{role.roleName}" />
            <p:ajax listener="#{formSecurityAction.testing}" update="roleAction"></p:ajax>
        </p:selectCheckboxMenu>
        <p:dataTable id="roleAction" var="selectedFormRole"
            value="#{formSecurityBean.selectedRoleList}">
            <p:column>
                <h:outputLabel value="#{selectedFormRole.roleName}" />
            </p:column>
        </p:dataTable>

监听器功能&#34; formSecurityAction.testing&#34;:

public void testing() {

    List<RoleResponseTO> selectedRoleList = new ArrayList<RoleResponseTO>();
    if (formSecurityBean.getRoleMulti() != null
            && formSecurityBean.getRoleMulti().length > 0) {
        for (int i = 0; i < formSecurityBean.getRoleMulti().length; i++) {
            selectedRoleList.add(formSecurityBean.getFormRoleMap().get(i));
        }
        formSecurityBean.setSelectedRoleList(selectedRoleList);
    }

    if (formSecurityBean.getSelectedRoleList() != null) {
        System.out.println(formSecurityBean.getSelectedRoleList().size());
        Iterator<RoleResponseTO> it = formSecurityBean
                .getSelectedRoleList().iterator();
        while (it.hasNext()) {
            RoleResponseTO tmp = it.next();
            System.out.println(tmp.getRoleName());      //HERE NullPointerException
        }
    }
}

我的问题: 我得到列表的大小&#34; formSecurityBean.getSelectedRoleList()。size()&#34; = 2,但是当我迭代它时,它会抛出NullPointerExeption。令我惊讶的是,它正在进入内部&#34;而(it.hasNext())&#34;循环条件。

堆栈跟踪的一部分如下:

20:26:25,505 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8080-1) 2

20:26:44,192 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (http-0.0.0.0-0.0.0.0-8080-1) /security/formSecurity.xhtml @75,75 listener="#{formSecurityAction.testing}": java.lang.NullPointerException: javax.el.ELException: /security/formSecurity.xhtml @75,75 listener="#{formSecurityAction.testing}": java.lang.NullPointerException
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at org.primefaces.behavior.ajax.AjaxBehaviorListenerImpl.processAjaxBehavior(AjaxBehaviorListenerImpl.java:53) [primefaces-5.1.jar:5.1]
    at javax.faces.event.AjaxBehaviorEvent.processListener(AjaxBehaviorEvent.java:113) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
    at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:106) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:760) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
.
.
Caused by: java.lang.NullPointerException
    at com.alt.security.controller.FormSecurityAction.testing(FormSecurityAction.java:352) [classes:]
    at com.alt.security.controller.FormSecurityAction$Proxy$_$$_WeldClientProxy.testing(FormSecurityAction$Proxy$_$$_WeldClientProxy.java) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_51]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_51]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_51]
    at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_51]
.
.

1 个答案:

答案 0 :(得分:0)

selectedRoleList.add(formSecurityBean.getFormRoleMap().get(i));

上面的代码行似乎有问题 您正在添加Null 2 Times
大小变为2,但当你调用toString()时,它仍然是空的,NPE即将到来。