2010-10-23 6 views
0

J'ai une application simple qui gère les équipes de football et les matchs. J'utilise JPA, sous la forme editMatch.jsp j'ai la propriété team_1, team_2 (instance de la classe Team) pour choisir l'équipe dans la liste. Le problème est lors de l'édition de match, le team_1 et team_2 ne sélectionnent pas dans la liste, et après avoir envoyé le message d'erreur est: Property team_1 jeté l'exception; L'exception imbriquée est java.lang.NullPointerException. Dans le contrôleur je lie team_1, team_2 et je suppose que l'erreur est quelque part entre la liaison et l'initialisation du formulaire.spring-mvc + jpa: liaison de données

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(): ""); 
} 

}

modifier:

errors.getFieldError ("TEAM_1"):

erreur sur le terrain dans l'objet 'match' sur le champ 'TEAM_1': valeur rejetée [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 []; message par défaut [team_1]]; message par défaut [Property 'team_1' a levé l'exception; exception imbriquée est java.lang.NullPointerException]

+1

Afficher la pile complète, s'il vous plaît. – axtavt

Répondre

0

instanciation TeamPropertyEditor mais n'appelez setManager() sur elle, de sorte que son champ manager est null, donc vous obtenez NPE lorsque vous essayez d'appeler manager.getTeamById(...).