简体   繁体   English

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

[英]In RichFaces, While using Email validation I am getting NullPointer Exception?

BeanClass code is: 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 Code is: .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"/>

Exception is: 例外是:

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

How can i rectify this! 我该如何纠正!

You need to validate the value which is been passed in as method argument, not the local variable email , simply because it isn't been set yet (it has first to be validated!). 您需要验证作为方法参数传入value ,而不是局部变量email ,这仅仅是因为尚未设置(必须首先对其进行验证!)。 It will be set only after validation phase, namely the update model values phase. 仅在验证阶段(即更新模型值阶段)之后设置。

So, replace 因此,更换

Matcher m = p.matcher(email);

by 通过

Matcher m = p.matcher(value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM