微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PrimeFaces 验证器属性方法中的 NPE

如何解决PrimeFaces 验证器属性方法中的 NPE

我尝试使用验证器属性验证 p:selectOneMenu,但在验证方法中,对象的值为空。我不明白为什么它是空的,有人可以帮助我。我使用的是 PrimeFaces 6.2。

我的 xhtml 是:

<p:selectOneMenu id="somFieldType"
                 widgetvar="somFieldType"
                 converter="entityConverter"
                 required="false"
                 value="#{paramDetail.article_field_type}"
                 validator="#{detailArticleBean2.isTypeValid}"
                 ... >
    <f:selectItem itemLabel="#{i18n['avocado.general.seleccionar']}" itemValue="0"/>
    <f:selectItem itemLabel="#{i18n['avocado.general.texto']}" itemValue="1"/>
    ...
</p:selectOneMenu>

在我的 bean 中,我只打印值:

public void isTypeValid(FacesContext ctx,UIComponent component,Object value) throws ValidatorException {
    System.out.println(value.toString());
}

控制台返回错误

javax.el.ELException: /Ref/Art/artDetail.xhtml @165,67 validator="#{detailArticleBean2.isTypeValid}": java.lang.NullPointerException
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)

非常感谢您的帮助。

解决方法

您的值为 null,因此您不能应用 .toString()。使用类似:

public void isTypeValid(FacesContext ctx,UIComponent component,Object value) throws ValidatorException {
    if (value == null) {
        // Value is unset; don't validate
        return;
    }
    // Value is set; do validation
    ...
}

另见:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。