购物车,Servlet如下:public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String foodID=request.getParameter("foodID");
HttpSession sess=request.getSession();
Map<String, CartItemBean> cart=(Map<String, CartItemBean>)sess.getAttribute("CarLog");
FoodBean foodBean=(FoodBean)sess.getAttribute("foodInfo");
if (foodBean==null) {
foodInfoDao fiDao=new foodInfoDaoImpl();
foodBean=fiDao.findFoodInfoByID(foodID);
}
if (cart==null) {
cart=new HashMap<String, CartItemBean>();
sess.setAttribute("CarLog", cart);
}
CartItemBean cartItemBean=(CartItemBean)cart.get(foodBean.getFoodID());
if (cartItemBean==null) {
cart.put(foodBean.getFoodID(), new CartItemBean(foodBean));
}
else {
cartItemBean.setQuantity(cartItemBean.getQuantity()+1);
}
request.getRequestDispatcher("shopCart.jsp").forward(request, response);
}JSP中的代码如下:<%
Map<String,CartItemBean> cart;
cart=(Map<String,CartItemBean>)session.getAttribute("CarLog");
if(cart==null||cart.size()==0)
{
out.println("<tr><td colspan='5'><a href='show.jsp'>您的购物车是空的,请返回选择商品</a></td></tr>");
}
else{
double total=0;
for(String key:cart.keySet())
{
CartItemBean cartItemBean=cart.get(key);
double subtotal=cartItemBean.getQuantity()*cartItemBean.getFood().getFoodPrice();
total+=subtotal;
%>
<tr height="25">
<td><%=cartItemBean.getFood().getFoodID() %></td>
<td><%=cartItemBean.getFood().getFoodName() %></td>
<td>¥<%=cartItemBean.getFood().getFoodPrice() %></td>
<td><%=cartItemBean.getQuantity() %></td>
<td>¥<%=subtotal %></td>
</tr>
<%} %>
<tr height="30">
<td width="60" bgcolor="#c0c0c0">合计</td>
<td width="500" bgcolor="#c0c0c0">-</td>
<td width="80" bgcolor="#c0c0c0">-</td>
<td width="80" bgcolor="#c0c0c0">-</td>
<td width="80" bgcolor="#c0c0c0">¥<%=total %></td>
</tr>
<%} %>
我不添加任何商品,按照思路来说应该是运行out.println("<tr><td colspan='5'><a href='show.jsp'>您的购物车是空的,请返回选择商品</a></td></tr>");可是总是报错,空指针异常严重: Servlet.service() for servlet AddFoodToCart threw exception
java.lang.NullPointerException
at restrant.src.Servlet.AddFoodToCart.doPost(AddFoodToCart.java:79)
at restrant.src.Servlet.AddFoodToCart.doGet(AddFoodToCart.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)求解~!!!!
throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String foodID=request.getParameter("foodID");
HttpSession sess=request.getSession();
Map<String, CartItemBean> cart=(Map<String, CartItemBean>)sess.getAttribute("CarLog");
FoodBean foodBean=(FoodBean)sess.getAttribute("foodInfo");
if (foodBean==null) {
foodInfoDao fiDao=new foodInfoDaoImpl();
foodBean=fiDao.findFoodInfoByID(foodID);
}
if (cart==null) {
cart=new HashMap<String, CartItemBean>();
sess.setAttribute("CarLog", cart);
}
CartItemBean cartItemBean=(CartItemBean)cart.get(foodBean.getFoodID());
if (cartItemBean==null) {
cart.put(foodBean.getFoodID(), new CartItemBean(foodBean));
}
else {
cartItemBean.setQuantity(cartItemBean.getQuantity()+1);
}
request.getRequestDispatcher("shopCart.jsp").forward(request, response);
}JSP中的代码如下:<%
Map<String,CartItemBean> cart;
cart=(Map<String,CartItemBean>)session.getAttribute("CarLog");
if(cart==null||cart.size()==0)
{
out.println("<tr><td colspan='5'><a href='show.jsp'>您的购物车是空的,请返回选择商品</a></td></tr>");
}
else{
double total=0;
for(String key:cart.keySet())
{
CartItemBean cartItemBean=cart.get(key);
double subtotal=cartItemBean.getQuantity()*cartItemBean.getFood().getFoodPrice();
total+=subtotal;
%>
<tr height="25">
<td><%=cartItemBean.getFood().getFoodID() %></td>
<td><%=cartItemBean.getFood().getFoodName() %></td>
<td>¥<%=cartItemBean.getFood().getFoodPrice() %></td>
<td><%=cartItemBean.getQuantity() %></td>
<td>¥<%=subtotal %></td>
</tr>
<%} %>
<tr height="30">
<td width="60" bgcolor="#c0c0c0">合计</td>
<td width="500" bgcolor="#c0c0c0">-</td>
<td width="80" bgcolor="#c0c0c0">-</td>
<td width="80" bgcolor="#c0c0c0">-</td>
<td width="80" bgcolor="#c0c0c0">¥<%=total %></td>
</tr>
<%} %>
我不添加任何商品,按照思路来说应该是运行out.println("<tr><td colspan='5'><a href='show.jsp'>您的购物车是空的,请返回选择商品</a></td></tr>");可是总是报错,空指针异常严重: Servlet.service() for servlet AddFoodToCart threw exception
java.lang.NullPointerException
at restrant.src.Servlet.AddFoodToCart.doPost(AddFoodToCart.java:79)
at restrant.src.Servlet.AddFoodToCart.doGet(AddFoodToCart.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)求解~!!!!
解决方案 »
- Java web 网站的配置
- 关于文件读取的操作
- 急~~~需要一个经典的OA和EJB实例系统 并附有教程及源码 最好能帮我详细解释下!!!
- 求IM即时通讯软件开发。
- JDK6的xjc中,如何调用已有的class型定义schema?
- 关于tomcat的问题!!急!!高手请进
- Vector的用法?在线等
- 在java中: byte[] tBytes = (new String(encryptText)).getBytes();
- 50分 求功能最全的log4j配置文件
- No Hibernate Session bound to thread, and configuration does not allow creation
- 关于EJB3.0的一个问题
- 收到offer 宇易信诚
at restrant.src.Servlet.AddFoodToCart.doPost(AddFoodToCart.java:79)
就是这一句话
实例化了一个购物车对象,然后接收Map获取的session,根据id来查找是否在购物车中存在
CartItemBean cartItemBean=(CartItemBean)cart.get(foodBean.getFoodID());
在(AddFoodToCart.java:79)这句前面打上这两句 看看是不是有 null 的
System.out.println("cart====="+cart);
System.out.println(""foodBean===+foodBean);null.get 就会 java.lang.NullPointerException
因为在Servlet中 FoodBean foodBean=(FoodBean)sess.getAttribute("foodInfo");
没有取到值,所以会报错,加一个if判断就可以了~~ 谢谢各位
System.out.println("foodBean==="+foodBean);