I'm beginner with Java EE, when I try to transfer an object between a JSP and a Servlet I use a session. For an homework, I have to create an e-commerce website. I just have this in my jsp:
HttpSession sess = request.getSession();
Panier pan = new Panier();
sess.setAttribute("panier", pan);
sess.setAttribute("produit", produits.get(0));
produits is an arraylist of "produit" object.
In my Servlet:
HttpSession sess = request.getSession();
Panier pan = (Panier) sess.getAttribute("panier");
Produit p1 = (Produit) sess.getAttribute("prod");
When I'm here, all works cause I can display the good attributes of the object pan or p1. But, when I use my method "ajouterProduit" (addProduct in English), a savage null pointer exception appears.
My class "Produit":
public class Produit implements java.io.Serializable{
private String nom;
private int prix;
public Produit(String name, int price){
this.nom = name;
this.prix = price;
}
public void setNom(String n){
this.nom = n;
}
public void setPrix(int p){
this.prix = p;
}
public String getNom(){
return this.nom;
}
public int getPrix(){
return this.prix;
}
}
My class "Panier" (basket in english):
public class Panier implements Serializable{
private List<Produit> panier;
private static int montant = 0;
public Panier(){
panier = new ArrayList<>();
}
public void ajouterProduit(Produit p1){
panier.add(p1);
montant = montant + p1.getPrix();
}
public void ajouterProduit(Produit p1, Produit p2){
panier.add(p1);
montant = montant + p1.getPrix();
panier.add(p2);
montant = montant + p2.getPrix();
}
public void ajouterProduit(Produit p1, Produit p2,Produit p3){
panier.add(p1);
montant = montant + p1.getPrix();
panier.add(p2);
montant = montant + p2.getPrix();
panier.add(p3);
montant = montant + p3.getPrix();
}
public List<Produit> getPanier(){
return panier;
}
public int getMontantTotal(){
return montant;
}
}
Thanks in advance for help ! ;)