jburns
Nov 27th, 2004, 08:27 PM
Hi. I'm trying to compare two dates upon validation to ensure that one date is after the other, but I keep getting an error. If I don't specify any conditions in the validator for these date fields, and no dates are selected, I get an error reported back that I pick up with status.errorMessage in the bind tag (I assume because the object couldn't bind without the dates being entered). However once I add conditions into my validator object to compare them (to ensure that one date follows another), I get a nullpointerexeption:
my init binder:
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) {
NumberFormat nf = NumberFormat.getNumberInstance();
// must follow this in request for bind to work: "Aug 18, 2004 1:31:44 PM"
DateFormat df = DateFormat.getDateTimeInstance();
binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, nf, true));
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(df, false));
}
in my Validator object:
public void validate(Object obj, Errors errors) {
Project project = (Project)obj;
if(project == null) {
errors.rejectValue("name", "error.not-specified", null, "Invalid Project Entry");
log.debug("--------------------------------project == null-----------------------------");
}
else {
log.debug("--------------------Validating with " + project + ": " + project.getName() + "----------------");
if(project.getDescription() == null || "".equals(project.getDescription())) {
errors.rejectValue("description", "errors.required", new Object[] {new String("Description")}, "A Description is Required");
}
if(project.getName() == null || "".equals(project.getName())) {
errors.rejectValue("name", "errors.required", new Object[] {new String("Name")}, "A Project Name is Required");
}
/*
if(project.getCompletion_date().before(project.get Create_date())) {
errors.rejectValue("completion_date", "errors.date-toosoon", new Object[] {new String(project.getCompletion_date().toString()), new String(project.getStart_date().toString())}, "Date too soon");
}
*/
}
}
the error upon post:
DEBUG - ValidationUtils.invokeValidator(50) | Invoking validator [com.projectscribe.web.ProjectFormValidator@27de24]
DEBUG - ProjectFormValidator.validate(41) | --------------------Validating with com.projectscribe.model.Project@156c69c: lkj----------------
ERROR - FrameworkServlet.service(342) | Could not complete request
java.lang.NullPointerException
at com.projectscribe.web.ProjectFormValidator.validat e(ProjectFormValidator.java:49)
at org.springframework.validation.ValidationUtils.inv okeValidator(ValidationUtils.java:56)
at org.springframework.web.servlet.mvc.BaseCommandCon troller.bindAndValidate(BaseCommandController.java :298)
at org.springframework.web.servlet.mvc.AbstractFormCo ntroller.handleRequestInternal(AbstractFormControl ler.java:236)
at org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:128)
at org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:44)
at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:522)
at org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:321)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
Any thoughts/ideas? Is there a better way to approach this?
my init binder:
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) {
NumberFormat nf = NumberFormat.getNumberInstance();
// must follow this in request for bind to work: "Aug 18, 2004 1:31:44 PM"
DateFormat df = DateFormat.getDateTimeInstance();
binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, nf, true));
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(df, false));
}
in my Validator object:
public void validate(Object obj, Errors errors) {
Project project = (Project)obj;
if(project == null) {
errors.rejectValue("name", "error.not-specified", null, "Invalid Project Entry");
log.debug("--------------------------------project == null-----------------------------");
}
else {
log.debug("--------------------Validating with " + project + ": " + project.getName() + "----------------");
if(project.getDescription() == null || "".equals(project.getDescription())) {
errors.rejectValue("description", "errors.required", new Object[] {new String("Description")}, "A Description is Required");
}
if(project.getName() == null || "".equals(project.getName())) {
errors.rejectValue("name", "errors.required", new Object[] {new String("Name")}, "A Project Name is Required");
}
/*
if(project.getCompletion_date().before(project.get Create_date())) {
errors.rejectValue("completion_date", "errors.date-toosoon", new Object[] {new String(project.getCompletion_date().toString()), new String(project.getStart_date().toString())}, "Date too soon");
}
*/
}
}
the error upon post:
DEBUG - ValidationUtils.invokeValidator(50) | Invoking validator [com.projectscribe.web.ProjectFormValidator@27de24]
DEBUG - ProjectFormValidator.validate(41) | --------------------Validating with com.projectscribe.model.Project@156c69c: lkj----------------
ERROR - FrameworkServlet.service(342) | Could not complete request
java.lang.NullPointerException
at com.projectscribe.web.ProjectFormValidator.validat e(ProjectFormValidator.java:49)
at org.springframework.validation.ValidationUtils.inv okeValidator(ValidationUtils.java:56)
at org.springframework.web.servlet.mvc.BaseCommandCon troller.bindAndValidate(BaseCommandController.java :298)
at org.springframework.web.servlet.mvc.AbstractFormCo ntroller.handleRequestInternal(AbstractFormControl ler.java:236)
at org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:128)
at org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:44)
at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:522)
at org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:321)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
Any thoughts/ideas? Is there a better way to approach this?