2

I try to validate a p:selectOneMenu using the validator attribute, but in the validate method the value of object is null. I don´t understand why it is null, somebody could help me. I´m using PrimeFaces 6.2.

My xhtml is:

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

In my bean I´m only printing the value:

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

The console returns an error:

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)

I´d appreciate your help.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
EdgKrg27
  • 23
  • 3

1 Answers1

3

Your value is null, so you cannot apply .toString(). Use something like:

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

See also:

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102