[JSP / Servlet] setAttribut java.lang.nullpointerexception
salut,
j'ai rencontré un probleme en essayant de creer un attribut contenant l'id de l'element selectionner dans une liste au niveau de ma jsp
puis le passer a ma servlet..
merci de votre aide
JSP:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <form method="post" action="<c:url value="/creationCommandeExped"/>">
<fieldset>
<legend>Création d'une commande</legend>
<fieldset>
<legend>Informations client</legend>
<%-- Si et seulement si la liste des clients n'est pas vide, alors on propose un choix à l'utilisateur --%>
<c:if test="${ !empty clientService.findAll() }">
<label for="choixNouveauClient">Nouveau client ? <span class="requis">*</span></label>
<input type="radio" id="choixNouveauClient" name="choixNouveauClient" value="nouveauClient" checked /> Oui
<input type="radio" id="choixNouveauClient" name="choixNouveauClient" value="ancienClient" /> Non <br/><br />
</c:if>
<c:set var="idClt" value="${ clients.idClient }" scope="request" />
<div id="nouveauClient">
<c:import url="/creerClient.jsp" />
</div>
<%-- Si et seulement si la liste des clients n'est pas vide, alors on crée la liste déroulante --%>
<c:if test="${ !empty clientService.findAll() }">
<div id="ancienClient">
<select name="listeClients" id="listeClients">
<option value="" disabled selected>Choisissez un client...</option>
<%-- Boucle sur la map des clients --%>
<c:forEach items="${ clientService.findAll() }" var="clients">
<option value="${ clients.idClient }">${clients.nom } ${ clients.prenom }</option>
<c:set var="idClt" value="${ clients.idClient }" scope="request" />
</c:forEach>
</select>
</div>
</c:if>
</fieldset><input class="bouton" type="submit" value="Valider" /> <input class="bouton" type="reset" value="Remettre à zéro" /> <br />
</form> |
servlet methode doPost :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
{
CommandeExpedService ces = new CommandeExpedService();
ClientService cs = new ClientService();
// objet de gestion de creation de commande d'expedition
CreationCommandeExpedGst cceg = new CreationCommandeExpedGst();
// traitement de la requete et recuperation de la commande
CommandeExped commandeExped = cceg.creerCommande( request );
/* Ajout du bean et de l'objet métier à l'objet requête */
request.setAttribute( ATT_COMMANDE, commandeExped );
request.setAttribute( ATT_COMMGST, cceg );
// Si aucune erreur
if ( cceg.getErreurs().isEmpty() ) {
cs.create( commandeExped.getClient() );
ces.create( commandeExped );
this.getServletContext().getRequestDispatcher( VUE_SUCCES ).forward( request, response );
} else {
this.getServletContext().getRequestDispatcher( VUE_FORM ).forward( request, response );
}
} |
la classe CreationCommandeExpedGst appelé dans ma servlet:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| public class CreationCommandeExpedGst {
private static final String CHAMP_CHOIX_CLIENT = "choixNouveauClient";
private static final String FORMAT_DATE = "dd/MM/yyyy HH:mm:ss";
private static final String ANCIEN_CLIENT = "ancienClient";
private static final String CHAMP_QUANTITE = "quantite";
private static final String CHAMP_CHOIX_LIVRAISON = "choixLivraison";
private static final String CHAMP_DATEEXPED = "dateExped";
private String resultat;
private Map<String, String> erreurs = new HashMap<String, String>();
public Map<String, String> getErreurs() {
return erreurs;
}
public String getResultat() {
return resultat;
}
public CommandeExped creerCommande( HttpServletRequest request ) {
Client client;
Produit produit;
ClientService cs = new ClientService();
ProduitService ps = new ProduitService();
// Si l'utilisateur choisit un client déjà existant, pas de validation à
// effectuer
String choixNouveauClient = getValeurChamp( request, CHAMP_CHOIX_CLIENT );
if ( ANCIEN_CLIENT.equals( choixNouveauClient ) ) {
/* Récupération du nom du client choisi */
int id = (Integer) request.getAttribute( "idClt" );
client = cs.findById( id );
} else {
/*
* Sinon on garde l'ancien mode, pour la validation des champs.
*/
CreationClientGst clientGst = new CreationClientGst();
client = clientGst.creerClient( request );
/*
* récupérer le contenu de la map d'erreur créée par l'objet métier
* CreationClientGst dans la map d'erreurs courante, actuellement
* vide.
*/
erreurs = clientGst.getErreurs();
}
...
...
...} |
l'erreur est levé au niveau de :
Code:
int id = (Integer) request.getAttribute( "idClt" );
merci encore..