在RichFaces中,在使用电子邮件验证时,我收到了NullPointer异常?

时间:2010-10-15 11:37:41

标签: java jsf xhtml richfaces

BeanClass代码是:

       public void validEmail(FacesContext context, UIComponent component,
                Object value) throws Exception  
       {

                  //Set the email pattern string
          Pattern p = Pattern.compile(".+@.+\\.[a-z]+");

          //Match the given string with the pattern
          Matcher m = p.matcher(email);

          //check whether match is found 
          boolean matchFound = m.matches();

          if (matchFound)
            System.out.println("Valid Email Id.");
          else
            System.out.println("Invalid Email Id.");
       }

.xhtml代码是:

<h:inputText title="Enter Email Address" value="#{registerBean.email}" id="eMail" required="true" validator="#{registerBean.validEmail}">
<rich:ajaxValidator event="oninputblur"/>

</h:inputText>
<rich:message for="eMail"/>

例外是:

/Register.xhtml @68,138 validator="#{registerBean.validEmail}": java.lang.NullPointerException

我该如何纠正这个问题呢?

1 个答案:

答案 0 :(得分:1)

您需要验证传入作为方法参数的value,而不是局部变量email,因为它尚未设置(它)首先要验证!)。它将仅在验证阶段之后设置,即更新模型值阶段。

所以,替换

Matcher m = p.matcher(email);

通过

Matcher m = p.matcher(value);