0 Replies Latest reply on Nov 2, 2010 7:29 AM by paul.dijou

    NullPointerException in s:validateEquality

    paul.dijou

      Hi,


      I am currently using Seam 2.2 and facing a problem with validation. I have two field which content percent values so I want their range between 0 and 100. So, in my Product class, I use @Range annotation.



      public class Produit implements Serializable
      {
           @Range(min = 0, max = 100)
           private Double percentMin;
           
           @Range(min = 0, max = 100)
           private Double percentMax;
      }





      In addition, the second field needs to be greater or equal than the first one. So, in my page, I use <s:validateEquality/> tag :




      <s:decorate id="percentMin" template="/layout/field/edit.jspx">
           <ui:define name="label">#{messages['myPage.label.percentMin']}</ui:define>
           <h:inputText value="#{product.percentMin}"
                id="percentMinInput"
                label="#{messages['myPage.label.percentMin']}"
                required="true">
                
           </h:inputText>
      </s:decorate>
      
      <s:decorate id="percentMax" template="/layout/field/edit.jspx">
           <ui:define name="label">#{messages['myPage.label.percentMax']}</ui:define>
           <h:inputText value="#{product.percentMax}"
                id="percentMaxInput"
                label="#{messages['myPage.label.percentMax']}"
                required="true">
                
                <s:validateEquality for=":#{rich:clientId('percentMinInput"')}"
                     operator="greater_or_equal"/>
                
           </h:inputText>
      </s:decorate>





      The <s:decorate/> tag adds <s:validateAll/> tags for all inputs. And here is the problem : if the first field percentMin is not in the range (for example with a value of -1), it returns a null value which is used in the <s:validateEquality/> and, of course, throw a NullPointerException. If we look at the EqualityValidator class on Seam, we can see that :




      public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
      {
           ...
           if (value == null && other == null)
           {
                // Thats fine
           }
           else if (value != null)
           {
                // Do the test
           }
      }



      Where value corresponds to the percentMax field value and other to the percentMin field value. So it's great to verify that value is not null but, in my opinion, if other is null (but not value), only the EQUAL and NOT_EQUAL tests should happen, not the others which invoque the compare method because it throws an exception.


      If someone has a solution for validate correctly my field, thanks a lot.