6 Replies Latest reply on Mar 19, 2008 3:39 AM by Wang Ruifeng

    About Custom Validator

    Wang Ruifeng Newbie

      I got java.lang.NullPointerException when i run my program.



      org.validator.IdExistValidator.isValid(IdExistValidator.java:20)
              at org.hibernate.validator.ClassValidator.getPotentialInvalidValues(ClassValidator.java:478)
              at org.jboss.seam.core.Validators$ValidatingResolver.setValue(Validators.java:199)
              at org.jboss.el.parser.AstPropertySuffix.setValue(AstPropertySuffix.java:73)
              at org.jboss.el.parser.AstValue.setValue(AstValue.java:84)
              at org.jboss.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:249)
              at com.sun.facelets.el.TagValueExpression.setValue(TagValueExpression.java:93)
              at org.jboss.seam.core.Validators.validate(Validators.java:140)
              at org.jboss.seam.ui.validator.ModelValidator.validate(ModelValidator.java:35)
              at javax.faces.component.UIInput.validateValue(UIInput.java:1013)
              at javax.faces.component.UIInput.validate(UIInput.java:867)
              at javax.faces.component.UIInput.executeValidate(UIInput.java:1065)
              at javax.faces.component.UIInput.processValidators(UIInput.java:666)
              at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1033)
              at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1033)
              at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1033)
              at javax.faces.component.UIForm.processValidators(UIForm.java:229)
              at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1033)
              at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:662)
              at org.ajax4jsf.component.AjaxViewRoot.access$201(AjaxViewRoot.java:57)
              at org.ajax4jsf.component.AjaxViewRoot$3.invokeRoot(AjaxViewRoot.java:319)
              at org.ajax4jsf.context.JsfOneOneInvoker.invokeOnRegionOrRoot(JsfOneOneInvoker.java:56)
              at org.ajax4jsf.context.AjaxContextImpl.invokeOnRegionOrRoot(AjaxContextImpl.java:170)
              at org.ajax4jsf.component.AjaxViewRoot.processValidators(AjaxViewRoot.java:333)
              at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:100)


      Student.java


      @Entity
      public class Student implements Serializable {
           
          private String school_id;     
          
          @Id
          @IdExist
          @NotNull
          public String getSchool_id() {
              return school_id;
          }
          public void setSchool_id(String school_id) {
              this.school_id = school_id;
          }
      .......



      IdExist.java


      @ValidatorClass(IdExistValidator.class)
      @Target({ElementType.METHOD, ElementType.FIELD})
      @Retention(RUNTIME)
      @Documented
      public @interface IdExist {
          
          String message() default "{validator.idExist}";
      
      }



      IdExistValidator.java



      public class IdExistValidator implements Validator<IdExist>, Serializable {
      
          @In(create=true) EntityManager entityManager;
          
          public void initialize(IdExist parameters) {
              
          }
      
          public boolean isValid(Object value) {
              List existing = entityManager.createQuery(
                          "select s.school_id from Student s where school_id = :school_id"
                      ).setParameter("school_id", value).getResultList();
              if(existing.size() == 0)
                  return true;
              else 
                  return false;
          }
      
      }
      

        • 1. Re: About Custom Validator
          Joao Viragine Newbie

          Ruifeng,


          Where is annotations in IdExistValidator class?


          Cheers,
          JP

          • 2. Re: About Custom Validator
            Olivier Thierry Apprentice

            Don't know if it will help you, but I had to add a @BypassInterceptors annotation to my validator classes to make them work. It was a long time ago, so I am not sure I had the same error, but you should try this.

            • 3. Re: About Custom Validator
              Pawel Wrzeszcz Novice

              Hi Ruifeng,


              You can't use injection in a validator class. Use manual lookup instead (and add @BypassInterceptors annotation).


              Cheers,
              -Pawel

              • 4. Re: About Custom Validator
                Wang Ruifeng Newbie

                Joao Viragine wrote on Mar 18, 2008 03:08 PM:


                Ruifeng,

                Where is annotations in IdExistValidator class?

                Cheers,
                JP


                Thanks.
                I use these code as a Hibernate Custom Validator.
                So I thing I don't need any annotations in IdExistValidator class.


                I read source code of Hibernate-Validator-3.
                I didn't see any annotations in these code.


                Here is some code from Hibernate-Validator.


                //$Id: FutureValidator.java 11282 2007-03-14 22:05:59Z epbernard $
                package org.hibernate.validator;
                
                import java.io.Serializable;
                import java.text.DateFormat;
                import java.text.ParseException;
                import java.util.Calendar;
                import java.util.Date;
                
                /**
                 * Check that a given date is in the future, and apply the same restriction
                 * at the DB level
                 *
                 * @author Emmanuel Bernard
                 */
                public class FutureValidator implements Validator<Future>, Serializable {
                
                     public void initialize(Future parameters) {
                     }
                
                     public boolean isValid(Object value) {
                          if ( value == null ) return true;
                          if ( value instanceof Date ) {
                               Date date = (Date) value;
                               return date.after( new Date() );
                          }
                          else if ( value instanceof Calendar ) {
                               Calendar cal = (Calendar) value;
                               return cal.after( Calendar.getInstance() );
                          }
                          else {
                               return false;
                          }
                     }
                }


                • 5. Re: About Custom Validator
                  Wang Ruifeng Newbie

                  I let it work by manual lookup the EntityManager.
                  And don't need add @BypassInterceptors annotation.


                  Thank you both for your replies.

                  • 6. Re: About Custom Validator
                    Wang Ruifeng Newbie

                    I get new problem.
                    When I add the Validator to my entity bean that need to be validate.
                    When I give the existed schoolid it can work well.The validate can tell user the id he wants to register has benn registered.
                    But when I give the non-exist school
                    id,I get some strange exception.



                    10:27:23,658 FATAL [application] java.lang.reflect.InvocationTargetException
                    javax.faces.el.EvaluationException: java.lang.reflect.InvocationTargetException
                            at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:91)
                            at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
                            at javax.faces.component.UICommand.broadcast(UICommand.java:383)
                            at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:184)
                            at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:162)
                            at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:350)
                            at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
                            at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
                            at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
                            at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
                            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                            at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                            at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
                            at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                            at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
                            at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                            at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                            at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                            at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
                            at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
                            at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
                            at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                            at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
                            at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                            at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
                            at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                            at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
                            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                            at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
                            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                            at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
                            at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
                            at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
                            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                            at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
                            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
                            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
                            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
                            at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
                            at java.lang.Thread.run(Thread.java:619)
                    Caused by: java.lang.reflect.InvocationTargetException
                            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                            at java.lang.reflect.Method.invoke(Method.java:597)
                            at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
                            at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
                            at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
                            at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:46)
                            at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                            at org.jboss.seam.persistence.ManagedEntityIdentityInterceptor.aroundInvoke(ManagedEntityIdentityInterceptor.java:48)
                            at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                            at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
                            at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                            at org.jboss.seam.core.ConversationInterceptor.aroundInvoke(ConversationInterceptor.java:65)
                            at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                            at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:38)
                            at org.jboss.seam.util.Work.workInTransaction(Work.java:40)
                            at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:32)
                            at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                            at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
                            at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                            at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
                            at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:166)
                            at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)
                            at org.oracleclub.session.beans.StudentHome_$$_javassist_1.persist(StudentHome_$$_javassist_1.java)
                            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                            at java.lang.reflect.Method.invoke(Method.java:597)
                            at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:329)
                            at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:342)
                            at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58)
                            at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
                            at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
                            at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
                            at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:77)
                            ... 46 more
                    Caused by: java.lang.StackOverflowError
                            at java.util.HashMap.get(HashMap.java:300)
                            at org.jboss.seam.contexts.BasicContext.get(BasicContext.java:48)
                            at org.jboss.seam.contexts.Contexts.lookupInStatefulContexts(Contexts.java:159)
                            at org.jboss.seam.Component.getInstance(Component.java:1839)
                            at org.jboss.seam.Component.getInstance(Component.java:1834)
                            at org.oracleclub.validator.IdExistValidator.isValid(IdExistValidator.java:17)
                            at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:341)
                            at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:307)
                    ......