0

I have Primefaces "selectCheckboxMenu" and want to show selected checkbox in a table dynamically.

My xhtml file content is :

        <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>

Listener function "formSecurityAction.testing":

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
        }
    }
}

My Problem : I am getting size of list "formSecurityBean.getSelectedRoleList().size()" = 2, but when I am iterating it, it throw NullPointerExeption. Adding to my surprise, it is coming inside "while(it.hasNext())" loop condition.

Part of stack-trace is as follow:

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]
.
.
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
user811602
  • 1,314
  • 2
  • 17
  • 47
  • There is no NPE in `p:ajax. Afaics there is an NPE in your code – Kukeltje Apr 26 '15 at 12:22
  • @Kukeltje, I think this NPE is linked with lifecycle of jsf (something like concurrent access /modification of list) is causing this. Otherwise, code execution will not go in while loop – user811602 Apr 26 '15 at 12:25
  • If it enters the method and there is an NPE there it still is in your code. Might be related to some lifecycle thing but still your code. Create an mcve and pinpoint the exact location of the NPE. After all that analysis (and that is what the duplicat explains) get back here with a specific question – Kukeltje Apr 26 '15 at 12:34

1 Answers1

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

There seems to be issue in above line of code.
You are adding Null 2 Times
Size becomes 2, But Its Still Null and NPE is coming when u call toString() on it.

Karan Kaw
  • 510
  • 1
  • 7
  • 15
  • thx a lot. You are right. This is what happen when you sit for hours on same line of code, thinking all possibility and forgetting basics. I want to delete this question. Is this right policy in SO to delete the question if i consider it valueless ?? – user811602 Apr 26 '15 at 12:48
  • It indeed is without any value (and my duplication suggestion was right) not accepting the answer and having it marked duplicate would have been better I think. Afaik there are automatic services that remove them then. Not sure if that happens when accepted – Kukeltje Apr 26 '15 at 14:01