You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Raible, Matt" <Ma...@cable.comcast.com> on 2003/05/30 15:13:22 UTC

Using the Validator outside of Struts

I'm trying to use the Validator to validate that a user has entered their
information in a self-registration servlet.

However, I'm getting a NullPointerException at the following line when
running a test using Cactus:

        InputStream rules =
 
getServletContext().getResourceAsStream("/WEB-INF/validator-rules.xml");

Error:

    [junit] java.lang.NullPointerException
    [junit]     at
javax.servlet.GenericServlet.getServletContext()Ljavax.servlet.ServletContex
t;(Ge
nericServlet.java:204)
    [junit]     at
com.comcast.cable.dmc.itd.cct.webapp.action.RegistrationServlet.execute(Ljav
ax.se
rvlet.http.HttpServletRequest;Ljavax.servlet.http.HttpServletResponse;)V(Reg
istrationServlet.java:12
2)

Full code:

        ValidatorResources resources = new ValidatorResources();

        InputStream rules =
 
getServletContext().getResourceAsStream("/WEB-INF/validator-rules.xml");

        ValidatorResourcesInitializer.initialize(resources, rules);

        InputStream forms =
 
getServletContext().getResourceAsStream("/WEB-INF/validation.xml");

        ValidatorResourcesInitializer.initialize(resources, forms);

        // Create a validator with the userForm
        Validator validator = new Validator(resources, "userFormEx");

        // Tell the validator which bean to validate against.  
        validator.addResource(Validator.BEAN_KEY, userForm);

        // Validate the checkoutForm object and store the validation results
        try {
            // Execute the validation rules
            ValidatorResults results = validator.validate();

            if (log.isDebugEnabled()) {
                log.debug(results.getResultValueMap());
            }
        } catch (ValidatorException ex) {
            // Log the validation exception
            log.warn("A validation exception occurred", ex);
        }

Any help is appreciated.

Thanks,

Matt


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org