I have simple application which manages football teams and matches. I am using JPA, in the form editMatch.jsp i have property team_1, team_2 (instance of class Team) for choosing the team from the list. The problem is when editing match, the team_1 and team_2 dont select in the list, and after submitting the error message is: Property team_1 threw exception; nested exception is java.lang.NullPointerException. In the controller I am binding team_1, team_2 and I suppose that the error is somewhere between binding and initialization of the form.
editMatch.jsp
<form:select path="team_1">
<form:options items="${teamList}" itemLabel="name" itemValue="id"/>
</form:select>
EditMatchController
public class EditMatchController extends SimpleFormController {
private MatchManager manager;
public EditMatchController() {}
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
Match match = (Match)binder.getTarget();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
try{
binder.registerCustomEditor(Date.class, "datum", new CustomDateEditor(sdf, false));
} catch(Exception e){}
binder.registerCustomEditor(Team.class, new TeamPropertyEditor());
binder.registerCustomEditor(Team.class, new TeamPropertyEditor());
}
@Override
protected Map referenceData(HttpServletRequest request) throws Exception {
Map<Object, Object> dataMap = new HashMap<Object, Object>();
dataMap.put("teamList", manager.getTeams());
return dataMap;
}
@Override
protected Object formBackingObject(HttpServletRequest request) throws Exception {
int idMatch = Integer.parseInt(request.getParameter("id"));
Match match_d = manager.getMatchById(idMatch);
if (match_d == null) {
throw new GenericException("Neplatný záznam.");
}
return match_d;
}
@Override
protected ModelAndView onSubmit(
HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws Exception {
Match match = (Match)command;
manager.updateMatch(match);
RedirectView redirect = new RedirectView(getSuccessView());
return new ModelAndView(redirect).addObject("message", match);
}
public void setManager(MatchManager manager) {
this.manager = manager;
}
}
TeamPropertyEditor
public class TeamPropertyEditor extends PropertyEditorSupport {
private MatchManager manager;
public void setManager(MatchManager manager) {
this.manager = manager;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text != null && text.length() > 0) {
try {
Team team = this.manager.getTeamById(new Integer(text));
super.setValue(team);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException();
}
} else {
super.setValue(null);
}
}
@Override
public String getAsText() {
Team team = (Team) super.getValue();
return (team != null ? (team.getId()+"").toString(): "");
}
}
edit:
errors.getFieldError("team_1"):
Field error in object 'match' on field 'team_1': rejected value [6]; codes [methodInvocation.match.team_1,methodInvocation.team_1,methodInvocation.model.Team,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [match.team_1,team_1]; arguments []; default message [team_1]]; default message [Property 'team_1' threw exception; nested exception is java.lang.NullPointerException]