Partage
  • Partager sur Facebook
  • Partager sur Twitter

Besoin d'aide sur la recuperation de l'iid

CreationCommande threw exception java.lang.NullPoi

Sujet résolu
    29 octobre 2014 à 19:27:54

    Bjr tout le oonde, voila j'ai un petit soucis, je suis sur le tutoriel  du JEE de Coyote  sur le tp-fil Etape 6,   impossible pour moi de creer la commande , j'ai cette erreur : "CreationCommande threw exception java.lang.NullPointerException"
    l'application tente d'utiliser un objet null alors qu'un objet est nécessaire. D'apres ce que j'aurai a moins que je me trompe, Jai ci dessous joint la parite du projet concernE. Merci de m'aider car je suis bloquE epuis deux jours  . 

    package com.tp.servelets;
    
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    
    import com.tp.beans.Client;
    import com.tp.beans.Commande;
    import com.tp.dao.DAOFactory;
    import com.tp.dao.clientDao;
    import com.tp.dao.commandeDao;
    import com.tp.forms.creationCommandeForm;
    
    /**
     * Servlet implementation class CreationCommande
     */
    public class CreationCommande extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    	private static final String ATT_FORM = "form";
    	private static final String ATT_COMMANDE = "commande";
    	private static final String SESSION_COMMANDE = "commandes";
    	private static final String VUE_FORM = "/creationCommande.jsp";
    	private static final String VUE_SUCCES = "/listeCommandes.jsp";
    	private static final String SESSION_CLIENT = "clients";
    	private static final String CONF_ATT_DAO = "daoFactory";
    	private clientDao clientdao;
    	private commandeDao commandedao;
    
    	public void init() throws ServletException {
    		this.clientdao = ((DAOFactory) getServletContext().getAttribute(
    				CONF_ATT_DAO)).getClientDao();
    		this.commandedao = ((DAOFactory) getServletContext().getAttribute(
    				CONF_ATT_DAO)).getCommandeDao();
    
    	}
    
    	protected void doGet(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
    		this.getServletContext().getRequestDispatcher(VUE_FORM)
    				.forward(request, response);
    	}
    
    	protected void doPost(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
    
    		creationCommandeForm form = new creationCommandeForm(clientdao,
    				commandedao);
    		;
    		Commande commande = form.creerCommande(request);
    		request.setAttribute(ATT_COMMANDE, commande);
    
    		request.setAttribute(ATT_FORM, form);
    
    		if (form.getErreurs().isEmpty()) {
    
    			HttpSession session = request.getSession();
    
    			Map<Long, Client> clients = (HashMap<Long, Client>) session
    					.getAttribute(SESSION_CLIENT);
    
    			if (clients == null) {
    				clients = new HashMap<Long, Client>();
    
    			}
    	System.out.println("01==="+commande.getClient().getId()+"===");//+commande.getClient()
    			
    			
    			
    			clients.put(commande.getClient().getId(), commande.getClient());
    			session.setAttribute(SESSION_CLIENT, clients);
    
    			@SuppressWarnings("unchecked")
    			Map<Long, Commande> commandes = (HashMap<Long, Commande>) session
    					.getAttribute(SESSION_COMMANDE);
    			if (commandes == null) {
    				commandes = new HashMap<Long, Commande>();
    			}
    			commandes.put(commande.getId(), commande);
    			session.setAttribute(SESSION_COMMANDE, commandes);
    
    			this.getServletContext().getRequestDispatcher(VUE_SUCCES)
    					.forward(request, response);
    		} else {
    			this.getServletContext().getRequestDispatcher(VUE_FORM)
    					.forward(request, response);
    		}
    
    	}
    }
    


    package com.tp.forms;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    import org.joda.time.DateTime;
    
    import com.tp.beans.Client;
    import com.tp.beans.Commande;
    import com.tp.dao.clientDao;
    import com.tp.dao.commandeDao;
    import com.tp.forms.FormatValidationException;
    import com.tp.forms.creationClientForm;
    
    public class creationCommandeForm {
    
    	private static final String CHAMP_MONTANT = "montant";
    	private static final String CHAMP_MODE_LIVRAISON = "modeLivraison";
    	private static final String CHAMP_MODE_PAIEMENT = "modePaiement";
    	private static final String CHAMP_STATUT_LIVRAISON = "statutLivraison";
    	private static final String CHAMP_STATUT_PAIEMENT = "statutPaiement";
    	private commandeDao commandedao;
    	private clientDao clientdao;
    	private static final String CHAMP_CHOIX_CLIENT = "choixNouveauClient";
    	private static final String ANCIEN_CLIENT = "ancienClient";
    	private static final String CHAMP_LISTE_CLIENTS = "listeClients";
    	private String resultat;
    	private static final String SESSION_CLIENTS = "clients";
    	private Map<String, String> erreurs = new HashMap<String, String>();
    
    	public String getResultat() {
    		return resultat;
    	}
    
    	public void setResultat(String resultat) {
    		this.resultat = resultat;
    	}
    
    	public Map<String, String> getErreurs() {
    		return erreurs;
    	}
    
    	public void setErreurs(String nomChamp, String message) {
    		erreurs.put(nomChamp, message);
    	}
    
    	public creationCommandeForm(clientDao clientdao, commandeDao commandedao) {
    		// TODO Auto-generated constructor stub
    		this.commandedao = commandedao;
    		this.clientdao = clientdao;
    	}
    
    	public String getValeurChamp(HttpServletRequest request, String nomChamp) {
    		String valeur = request.getParameter(nomChamp);
    		if (valeur == null || valeur.trim().length() == 0) {
    			return null;
    		} else {
    			return valeur;
    		}
    	}
    
    	public Commande creerCommande(HttpServletRequest request) {
    
    		Client client;
    		String choixNouveauClient = getValeurChamp(request, CHAMP_CHOIX_CLIENT);
    		if (ANCIEN_CLIENT.equals(choixNouveauClient)) {
    			String idAncienClient = getValeurChamp(request, CHAMP_LISTE_CLIENTS);
    			Long id = null;
    			try {
    				id = Long.parseLong(idAncienClient);
    			} catch (NumberFormatException e) {
    				setErreurs(CHAMP_CHOIX_CLIENT,
    						"client inconnu, merci d'utiliser le formulaire prevu a cet effet");
    				id = 0L;
    			}
    			HttpSession session = request.getSession();
    			client = ((HashMap<Client, Client>) session
    					.getAttribute(SESSION_CLIENTS)).get(id);
    		} else {
    			creationClientForm form = new creationClientForm(clientdao);
    			client = form.creerClient(request);
    			erreurs = form.getErreurs();
    		}
    		DateTime date = new DateTime();
    		String modePaiement = getValeurChamp(request, CHAMP_MODE_PAIEMENT);
    		String modeLivraison = getValeurChamp(request, CHAMP_MODE_LIVRAISON);
    		String statutLivraison = getValeurChamp(request, CHAMP_STATUT_LIVRAISON);
    		String statutPaiement = getValeurChamp(request, CHAMP_STATUT_PAIEMENT);
    		String montant = getValeurChamp(request, CHAMP_MONTANT);
    		Commande commande = new Commande();
    
    		try {
    			commande.setDate(date);
    			traiterMontant(montant, commande);
    			traiter_Mode_Paiement(modePaiement, commande);
    			traiter_Mode_Livraison(modeLivraison, commande);
    			traiter_Statut_Paiement(statutPaiement, commande);
    			traiter_Statut_Livraison(statutLivraison, commande);
    			System.out.println(commande.getMontant()
    					+ commande.getStatutLivraison()
    					+ commande.getStatutPaiement()
    					+ commande.getModeLivraison() + commande.getModePaiement());
    			if (erreurs.isEmpty()) {
    
    				commandedao.creer(commande);
    				resultat = "Succes";
    			} else {
    				resultat = "Echec de la creation de la commande";
    			}
    
    		} catch (Exception e) {
    			// TODO: handle exception
    		}
    		return commande;
    
    	}
    
    	public double validationMontant(String montant)
    			throws FormatValidationException {
    
    		double temp;
    		if (montant != null) {
    			try {
    				temp = Double.parseDouble(montant);
    				if (temp < 0) {
    					throw new FormatValidationException(
    							"Le montant doit etre un nombre positif");
    				}
    
    			} catch (Exception e) {
    				// TODO: handle exception
    				temp = -1;
    				throw new FormatValidationException(
    						"Le montant doit etre un nombre");
    			}
    		} else {
    			temp = -1;
    			throw new FormatValidationException("Veuillez entrer le montant");
    		}
    		return temp;
    
    	}
    
    	public void validation_Mode_Paiement(String modePaiement)
    			throws FormatValidationException {
    		if (modePaiement != null) {
    			if (modePaiement.length() < 2) {
    				throw new FormatValidationException(
    						"Le champ doit contenir au moins caracteres");
    			}
    		} else {
    			throw new FormatValidationException(
    					"Veuillez entrer le mode de paiement");
    		}
    	}
    
    	public void validation_Mode_Livraison(String modeLivraison)
    			throws FormatValidationException {
    		if (modeLivraison != null) {
    			if (modeLivraison.length() < 2) {
    				throw new FormatValidationException(
    						"Le champ doit contenir au moins caracteres");
    			}
    		} else {
    			throw new FormatValidationException(
    					"Veuillez entrer le mode de livraison");
    		}
    	}
    
    	public void validation_Statut_Livraison(String statutLivraison)
    			throws FormatValidationException {
    		if (statutLivraison != null) {
    			if (statutLivraison.length() < 2) {
    				throw new FormatValidationException(
    						"Le champ doit contenir au moins caracteres");
    			}
    		} else {
    			throw new FormatValidationException(
    					"Veuillez entrer le statut de livraison");
    		}
    	}
    
    	public void validation_Statut_Paiement(String statutPaiement)
    			throws FormatValidationException {
    		if (statutPaiement != null) {
    			if (statutPaiement.length() < 2) {
    				throw new FormatValidationException(
    						"Le champ doit contenir au moins caracteres");
    			}
    		} else {
    			throw new FormatValidationException(
    					"Veuillez entrer le statut de paiement");
    		}
    	}
    
    	public void traiterMontant(String montant, Commande commande) {
    		double valeurMontant = 1;
    		try {
    			valeurMontant = validationMontant(montant);
    		} catch (Exception e) {
    			// TODO: handle exception
    			setErreurs(CHAMP_MONTANT, e.getMessage());
    		}
    		commande.setMontant(valeurMontant);
    
    	}
    
    	public void traiterClient(Client client, Commande commande) {
    		if (client == null) {
    			setErreurs(CHAMP_CHOIX_CLIENT,
    					"client inconnu, merci d'utiliser le formulaire prevu pour cet effet");
    		}
    		commande.setClient(client);
    
    	}
    
    	public void traiter_Mode_Paiement(String modePaiement, Commande commande) {
    		try {
    			validation_Mode_Paiement(modePaiement);
    		} catch (Exception e) {
    			// TODO: handle exception
    			setErreurs(CHAMP_MODE_PAIEMENT, e.getMessage());
    		}
    		commande.setModePaiement(modePaiement);
    	}
    
    	public void traiter_Mode_Livraison(String modeLivraison, Commande commande) {
    		try {
    			validation_Mode_Livraison(modeLivraison);
    		} catch (Exception e) {
    			// TODO: handle exception
    			setErreurs(CHAMP_MODE_LIVRAISON, e.getMessage());
    		}
    		commande.setModeLivraison(modeLivraison);
    	}
    
    	public void traiter_Statut_Paiement(String statutPaiement, Commande commande) {
    		try {
    			validation_Statut_Paiement(statutPaiement);
    		} catch (Exception e) {
    			// TODO: handle exception
    			setErreurs(CHAMP_STATUT_PAIEMENT, e.getMessage());
    		}
    		commande.setStatutPaiement(statutPaiement);
    	}
    
    	public void traiter_Statut_Livraison(String statutLivraison,
    			Commande commande) {
    		try {
    			validation_Statut_Livraison(statutLivraison);
    		} catch (Exception e) {
    			// TODO: handle exception
    			setErreurs(CHAMP_STATUT_LIVRAISON, e.getMessage());
    		}
    		commande.setStatutLivraison(statutLivraison);
    		System.out
    				.println("statutLivraison ==" + commande.getStatutLivraison());
    	}
    }







    • Partager sur Facebook
    • Partager sur Twitter
      31 octobre 2014 à 14:00:08

      Salut !

      Bon je suis cruel mais d'entrée : Les noms de classe, par convention, commencent toujours par une majuscule.

      Après ce court épisode résultant de ma lecture de ton code :p , on peut voir que pas mal de méthodes peuvent renvoyer null. Commences déjà par mettre des "System.out.println()" de ta variable "client" dans la méthode "creerCommande()". Cela permettra de savoir à partir de quel endroit ta variable devient null. Je suppose qu'elle vient de ta HashMap<Client, Client> (euh... curieux comme HashMap ça ^^) qui renvoie une valeur qui n'existait pas avant. D'autant plus que si une exception est capturée par un de tes "try/catch", la commande aura plein de champs null, et en plus elle est réceptionnée en aval, avec plein de valeurs null. Mieux vaut l'annuler en supprimant tes try/catch. et en mettant un "public void creerCommande() throws Exception". Tu pourras ensuite récupérer tes exception plus loin dans le code (ça clarifiera ton code en plus ;) ) .

      Enfin, on observe un code bien structuré en méthodes et en classes, mais ta méthode "creerCommande()" est désordonnée et assez confuse.

      -
      Edité par EclipseOnFire 31 octobre 2014 à 14:06:13

      • Partager sur Facebook
      • Partager sur Twitter
      Error 2006, MySQL server has gone away

      Besoin d'aide sur la recuperation de l'iid

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown