JSF 列表取决于返回 nullPointer 的选定值

JSF list depending on selected value returning nullPointer

在我的 jsf 页面的对话框中,我有一个 EntityB 列表,它取决于数据表中选定的 EntityA。 当我第一次加载页面时,它给了我 nullPpointer 异常,因为首先没有选择任何东西。谁能告诉我如何防止这种情况发生?

编辑:我向打开对话框的 link 添加了一个动作监听器 并收到此错误:

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>

豆子:

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

我现在正在使用的 Bean 侦听器:

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

        signaRender = true ;
    }

这是实体 B 列表的 getter,我正在寻找一种方法来获取空列表或仅在打开对话框时调用。

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

您还可以添加带有“Select”标签和空 itemValue 的 f:selectItem,例如 itemValue =“”。顺便说一句,永远不要有空列表,列表不应该为空,但它可以为空。这是最佳实践。因此,您可以在 bean 的 post 构造中将 listEntityB 初始化为一个空列表。

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