Il codice che ho scritto è:
Interfaccia Local per il carrello
codice:
package com.classes;
import javax.ejb.Local;
/**
*
* @author Proprietario
*/
@Local
interface CarrelloEJB{
public boolean addToCart(Strumenti s);
public boolean removeFromCart(Strumenti s);
public void eraseCart();
public float getTotal();
}
Clase Carrello:
codice:
package com.classes;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
/**
*
* @author Proprietario
*/
@Stateful
public class Carrello implements Serializable, CarrelloEJB{
protected HashMap<Strumenti, Integer> strumenti;
protected Utenti utente;
@PostConstruct
public void init(){
System.out.println("Carrello è stato creato");
strumenti=new HashMap<Strumenti, Integer>();
}
public boolean addToCart(Strumenti s){
if(strumenti.containsKey(s)){
//becco la quantità
int valore=strumenti.get(s).intValue();
System.out.println(valore);
//la incremento
valore++;
//faccio l'update
//tolgo il vecchio
strumenti.remove(s);
//metto il nuovo
strumenti.put(s, new Integer(valore));
//ritorno false perchè lo strumento c'è già
return false;
}//if c'è già
strumenti.put(s, new Integer(1));
return true;
}
public boolean removeFromCart(Strumenti s){
if (!strumenti.containsKey(s)) return false;//non posso rimuovere una cosa che non c'è
//becco la quantità
int valore=strumenti.get(s).intValue();
System.out.println(valore);
//la incremento
valore--;
//faccio l'update
//tolgo il vecchio
strumenti.remove(s);
//metto il nuovo se la quantità è almeno 1
if (valore>0) strumenti.put(s, new Integer(valore));
return true;
}//removeFromCart
public void eraseCart(){
strumenti=null;
}//eraseCart
public float getTotal(){
float totale=0;
//mi becco la lista (SET) degli strumenti dal set
Iterator lista_strumenti=strumenti.keySet().iterator();
//scorro la lista degli strumenti aggiunti
while(lista_strumenti.hasNext()){
//prendo lo strumento corrente
Strumenti curr=(Strumenti)lista_strumenti.next();
//prendo il suo prezzo
float prezzo=curr.getPrezzo();
//prendo la quantità dal carrello
int quantity=strumenti.get(curr).intValue();
//calcolo il subtotale per quello strumento
float subTotale=prezzo*quantity;
//aggiorno il totale
totale+=subTotale;
}//while
//ritorno il totale
return totale;
}
public HashMap<Strumenti, Integer> getCarrello(){
return strumenti;
}
public void setCarrello(){}
public Utenti getUtente(){
return utente;
}
public void setUtente(Utenti u){
this.utente=u;
}
}//end of the class Carrello
ora il mio problema è che ho creato una servlet che, come altre, mi servono per mettere e togliere gli oggetti dal carrello.
La servlet è per "aggiungi al carrello" ed è riportata di seguito:
codice:
package com.servlets.utenti;
import com.classes.Carrello;
import com.classes.Strumenti;
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Iterator;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.transaction.UserTransaction;
/**
*
* @author Proprietario
*/
public class AddToCart extends HttpServlet {
@PersistenceUnit(unitName="SM-JPAPU")
EntityManagerFactory emf;
@Resource
UserTransaction ut;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
EntityManager em=emf.createEntityManager();
try {
//System.out.println("AddToCart has been called");
int id=Integer.parseInt(request.getParameter("id"));
Strumenti s=em.find(Strumenti.class, id);
HttpSession sessione=request.getSession();
//come fare ad ottenere il carrello???
Carrello carrello=(Carrello) sessione.getAttribute("carrello");
if (carrello==null) carrello=new Carrello();
carrello.addToCart(s);
HashMap<Strumenti, Integer> contenuto=carrello.getCarrello();
String testo=repaintCarrello(contenuto);
sessione.setAttribute("carrello", carrello);
out.println(testo);
} catch(Exception ee){
ee.printStackTrace();
}
finally {
out.close();
}
}
private String repaintCarrello(HashMap<Strumenti, Integer> contenuto) {
Iterator content=contenuto.entrySet().iterator();
String html="";
while(content.hasNext()){
Strumenti curr=(Strumenti)content.next();
String nome=curr.getNome();
int id=curr.getId();
int quantity=contenuto.get(curr);
String questa_linea=""+nome+": "+quantity+"";
html+=questa_linea;
}//while
return html;
}
}
e viene chiamata da una funziona AJAX, che gli passa l'id dello strumento da aggiungere.
Quando provo ad aggiungere uno strumento al carrello mi viene sollevata l'eccezione:
codice:
java.lang.NullPointerException
at com.classes.Carrello.addToCart(Carrello.java:30)
at com.servlets.utenti.AddToCart.processRequest(AddToCart.java:53)
at com.servlets.utenti.AddToCart.doGet(AddToCart.java:89)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:718)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
dove sbaglio?
(NB:
Carrello.java: 30 è l'inizio del metodo addToCart, dove c'è l'if per vedee se lo strumento c'è già
AddToCart.java: 53 è la chiamata al metodo carrello.addToCart(s) nella servlet
)
grazie ancora e scusatemi per il post chilometrico!