简体   繁体   中英

JSF list depending on selected value returning nullPointer

In my jsf page, in a dialog, I have a list of EntityB which depends on selected EntityA in a datatable. When I first load the page it's giving me nullPpointer exception because nothing is selected in the first place. Can anyone tell me how to prevent this?

EDIT: I added an actionlistener to the link who open the dialog and getting this error:

Cannot convert DemandesDocsAdministratif type DemandesDocsAdministratif to class javax.faces.event.ActionEvent

JSF:

<p:commandLink value="#{demande.idDemandeDocAdministratif}"
                    oncomplete="PF('dlg2').show()" process="@this"
                    update=":form:pg" actionListener="#{gestionDemandesMB.fillEntityB}">
                    <f:setPropertyActionListener
                        target="#{gestionDemandesMB.SelectedEntityA}" value="#{demande}" 
                        />
                </p:commandLink>
<form>
<datatable>
</datatable>
    <p:dialog>
        <p:selectOneMenu id="Signataires"
                                value="#{gestionDemandesMB.entityB}">
                                <f:selectItems value="#{gestionDemandesMB.listEntityB}"
                                    var="sign" itemLabel="#{sign.libRole}"
                                    itemValue="#{sign.idPoste}" />
                            </p:selectOneMenu>
    </p:dialog>
</form>

Bean:

public List<EntityA> getListEntityB() {
    if ( selectedentityA != null ){
    return entityBService.ListByentityA(selectedEntityA)
            ; } else {
    return Collections.emptyList() ; }

Bean listener that I'm working with now:

public void fillSignataires(ActionEvent event)
    {
listB = entityBService.ListByentityA(selectedEntityA)

        signaRender = true ;
    }

this is the getter of entity B list, I'm looking for a way to either get an empty list or call only when I open dialog.

您可以使用rendered="#{not empty bean.list}"来防止selectOnMenu的呈现,直到您填充了对象。

You can also add af:selectItem with a "Select" label and empty itemValue such as itemValue = " ". By the way, never have a null list, list shouldn't be null but it can be empty. It's the best practice. So you can initialize your listEntityB as an empty list at post construct of your bean.

    <p:selectOneMenu id="Signataires"
                            value="#{gestionDemandesMB.entityB}">
                            <f:selectItem  itemLabel="Select" itemValue="" />
                                var="sign" itemLabel="#{sign.libRole}"
                            <f:selectItems value="#{gestionDemandesMB.listEntityB}"
                                var="sign" itemLabel="#{sign.libRole}"
                                itemValue="#{sign.idPoste}" />
                        </p:selectOneMenu>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM