Hello Everybody,
I am getting a NullPointerException when I am using a custom validator in my code. To be specific here is the detail:
Validation.xml snippet:
<formset>
<form name="adminLoginForm">
<field property="userName" depends="required, checkAuthorization">
<arg key="prompt.username"/>
<var>
<var-name>username</var-name>
<var-value>sharpie</var-value>
</var>
</field>
<field property="password" depends="required, checkAuthorization">
<arg key="prompt.password"/>
<var>
<var-name>password</var-name>
<var-value>sharpie</var-value>
</var>
</field>
</form>
</formset> Validator-rules.xml snippet:
<validator name="checkAuthorization"
classname="com.sharpie.common.utils.CustomValidator"
method="validateAuthorization"
methodParams="java.lang.Object,org.apache.commons.validator.Validator,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required">
</validator> Java code for CustomValidator:
public class CustomValidator implements Serializable {
private static final long serialVersionUID = 2765932841116180600L;
public boolean validateAuthorization(Object object, Validator validator, ValidatorAction validatorAction, Field field, ActionErrors errors, HttpServletRequest httpServletRequest) {
String userName = field.getVarValue("username");
if (userName!=null) {
String userNameValue = ValidatorUtils.getValueAsString(object, field.getProperty());
if(!userName.equals(userNameValue)) {
errors.add(field.getKey(),Resources.getActionMessage(validator,httpServletRequest,validatorAction,field));
return false;
}
}
String password = field.getVarValue("password");
if (password!=null) {
String passwordValue = ValidatorUtils.getValueAsString(object, field.getProperty());
if(!password.equals(passwordValue)) {
errors.add(field.getKey(),Resources.getActionMessage(validator,httpServletRequest,validatorAction,field));
return false;
}
}
return true;
}
} Please assume that the syntax are all perfect if you find any illegal ones. Any help on this matter would be greatly appreciated. Waiting for your reply.
Thanks
Raj