You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mi...@wellsfargo.com on 2004/12/10 03:15:38 UTC
Custom rule NullPointer errors
With the new version of Struts, we are getting a NullPointerException whenever custom rules are being run because they are expecting a parameter list of:
Object Bean,
ValidatorAction va,
Field field,
ActionErrors errors,
org.apache.commons.validator.Validator validator,
HttpServletRequest request
and the ActionErrors.errors object is being sent in as null. This did not happen in struts 1.1
Has the custom error handling for Struts changed in the new version?
Here are some of our rules:
=========================
<validator name="dateIsPast"
classname="com.wf.bd.ice.rules.validation.ICEFieldChecks"
method="validateDateIsPast"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
msg="errors.dateIsPast">
</validator>
And here is the Java Class:
======================
public static boolean validateDateIsPast(
Object bean, ValidatorAction va, Field field, ActionErrors errors,
org.apache.commons.validator.Validator validator,
HttpServletRequest request
) {
Object form =
validator.getResource(
org.apache.commons.validator.Validator.BEAN_KEY
);
String monthVal = null;
if(isString(bean)) {
monthVal = (String) bean;
} else {
monthVal =
ValidatorUtil.getValueAsString(bean, field.getProperty());
}
String yearVal = null;
if(!GenericValidator.isBlankOrNull(field.getVarValue("year"))) {
String yearProp = field.getVarValue("year");
yearVal = ValidatorUtil.getValueAsString(form, yearProp);
}
if((monthVal == null) || (yearVal == null)) {
return false;
}
int month;
int year;
try {
month = Integer.parseInt(monthVal);
year = Integer.parseInt(yearVal);
} catch(NumberFormatException nfe) {
return false;
}
// set the month-year date with the 1st of the month
Calendar then = new GregorianCalendar(year, month - 1, 1);
Calendar now = new GregorianCalendar();
if(then.after((Calendar) now)) {
errors.add(
field.getKey(), Resources.getActionError(request, va, field)
);
return false;
}
return true;
}
--------------------
Mick Knutson
Wells Fargo Business Direct Information Systems
(415) 222-1020
"This message may contain confidential and/or privileged information. If you are not the addressee or authorized to receive this for the addressee, you must not use, copy, disclose, or take any action based on this message or any information herein. If you have received this message in error, please advise the sender immediately by reply e-mail and delete this message. Thank you for your cooperation."
--------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Custom rule NullPointer errors
Posted by Tim Bailen <ti...@alliantenergy.com>.
<Mick.Knutson <at> wellsfargo.com> writes:
>
> With the new version of Struts, we are getting a NullPointerException
whenever custom rules are being run
> because they are expecting a parameter list of:
>
> Object Bean,
> ValidatorAction va,
> Field field,
> ActionErrors errors,
> org.apache.commons.validator.Validator validator,
> HttpServletRequest request
>
> and the ActionErrors.errors object is being sent in as null. This did not
happen in struts 1.1
>
> Has the custom error handling for Struts changed in the new version?
>
> Here are some of our rules:
> =========================
>
> <validator name="dateIsPast"
> classname="com.wf.bd.ice.rules.validation.ICEFieldChecks"
> method="validateDateIsPast"
> methodParams="java.lang.Object,
> org.apache.commons.validator.ValidatorAction,
> org.apache.commons.validator.Field,
> org.apache.struts.action.ActionErrors,
> org.apache.commons.validator.Validator,
> javax.servlet.http.HttpServletRequest"
> msg="errors.dateIsPast">
> </validator>
>
> And here is the Java Class:
> ======================
> public static boolean validateDateIsPast(
> Object bean, ValidatorAction va, Field field, ActionErrors errors,
> org.apache.commons.validator.Validator validator,
> HttpServletRequest request
> ) {
>
<snip>
>
> }
>
> --------------------
> Mick Knutson
> Wells Fargo Business Direct Information Systems
> (415) 222-1020
> --------------------
Mick, I noticed the same thing with our custom validator the other day. My
solution was to:
1. Use org.apache.struts.action.ActionMessages instead of
org.apache.struts.action.ActionErrors in the validator/@methodParams
2. Change your custom validator's method signature to use ActionMessages
instead of ActionErrors.
Best,
Tim
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Custom rule NullPointer errors
Posted by Laconia Data Systems <we...@laconiadatasystems.com>.
Mick-
The ActionForm.validate method
return an ActionErrors object that encapsulates any validation errors that
have been found. If no errors are found, return null or an ActionErrors
object with no recorded error messages.
The default implementation performs no validation and returns null.
Subclasses must override this method to provide any validation they wish to
perform.
take a look at
http://struts.apache.org/api/index.html
In other words override validate and handle the building of ActionErrors
object for any returned errors.
Martin-
----- Original Message -----
From: <Mi...@wellsfargo.com>
To: <us...@struts.apache.org>
Sent: Thursday, December 09, 2004 9:15 PM
Subject: Custom rule NullPointer errors
With the new version of Struts, we are getting a NullPointerException
whenever custom rules are being run because they are expecting a parameter
list of:
Object Bean,
ValidatorAction va,
Field field,
ActionErrors errors,
org.apache.commons.validator.Validator validator,
HttpServletRequest request
and the ActionErrors.errors object is being sent in as null. This did not
happen in struts 1.1
Has the custom error handling for Struts changed in the new version?
Here are some of our rules:
=========================
<validator name="dateIsPast"
classname="com.wf.bd.ice.rules.validation.ICEFieldChecks"
method="validateDateIsPast"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
msg="errors.dateIsPast">
</validator>
And here is the Java Class:
======================
public static boolean validateDateIsPast(
Object bean, ValidatorAction va, Field field, ActionErrors errors,
org.apache.commons.validator.Validator validator,
HttpServletRequest request
) {
Object form =
validator.getResource(
org.apache.commons.validator.Validator.BEAN_KEY
);
String monthVal = null;
if(isString(bean)) {
monthVal = (String) bean;
} else {
monthVal =
ValidatorUtil.getValueAsString(bean, field.getProperty());
}
String yearVal = null;
if(!GenericValidator.isBlankOrNull(field.getVarValue("year"))) {
String yearProp = field.getVarValue("year");
yearVal = ValidatorUtil.getValueAsString(form, yearProp);
}
if((monthVal == null) || (yearVal == null)) {
return false;
}
int month;
int year;
try {
month = Integer.parseInt(monthVal);
year = Integer.parseInt(yearVal);
} catch(NumberFormatException nfe) {
return false;
}
// set the month-year date with the 1st of the month
Calendar then = new GregorianCalendar(year, month - 1, 1);
Calendar now = new GregorianCalendar();
if(then.after((Calendar) now)) {
errors.add(
field.getKey(), Resources.getActionError(request, va, field)
);
return false;
}
return true;
}
--------------------
Mick Knutson
Wells Fargo Business Direct Information Systems
(415) 222-1020
"This message may contain confidential and/or privileged information. If you
are not the addressee or authorized to receive this for the addressee, you
must not use, copy, disclose, or take any action based on this message or
any information herein. If you have received this message in error, please
advise the sender immediately by reply e-mail and delete this message. Thank
you for your cooperation."
--------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org