当前位置:编程学习 > JAVA >>

韩顺平的网上商城,跟着视频做的,出现了空指针,请各位大牛解决一下,小弟在此谢过了

loginCl的代码

package com.sp.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sp.model.*;
public class loginCL extends HttpServlet {

/**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
//得到用户名和密码,验证
String u=request.getParameter("username");
String p=request.getParameter("password");

UserBeanBO ubb=new UserBeanBO();
if(ubb.checkUser(u,p)){
//1.把成功登陆的用户所有信息放入session
UserBean ub=ubb.getUserBean(u);
request.getSession().setAttribute("userInfo", ub);
//2.把购物车的信息取出
MycartBo mcb=(MycartBo)request.getSession().getAttribute("mycart");
ArrayList al=mcb.showMyCart();
//把al放入request
request.setAttribute("mycartinfo", al);

//用户合法
request.getRequestDispatcher("Shopping3.jsp").forward(request, response);

}else{
//用户不合法
request.getRequestDispatcher("shopping2.jsp").forward(request, response);
}

}

/**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doGet(request, response);
}

}


<%@ page language="java" import="java.util.*,com.sp.model.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//得到用户信息
UserBean ub=(UserBean)session.getAttribute("userInfo");
//得到购物车的信息
ArrayList al=(ArrayList)request.getAttribute("mycartinfo");
//得到购物车
MycartBo mcb=(MycartBo)session.getAttribute("mycart");

%>
shopping3.jsp:这个是用来显示订单的
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'shopping3.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<link rel="stylesheet" type="text/css" href="css/my.css">


  </head>
  
  <body topmargin="0" background="images/bg.jpg">
   <center>
   <td align="center">
   <jsp:include page="head.jsp"></jsp:include>
   </td>
  </tr>
  <tr>
    <td align="center"><table width="100%" border="1" cellspacing="0" cellpadding="0">
      <tr>
        <td align="center"><img src="images/gouwuliucheng.gif" width="406" height="50" /></td>
      </tr>
      <tr>
        <td align="center"><table width="71%" border="1" cellpadding="0" cellspacing="0" class="abc">
          <tr>
            <td colspan="2" align="center">购买人的信息</td>
            </tr>
          <tr>
            <td width="50%" align="right">用户名:</td>
            <td width="50%"><input type="text" name="textfield" value="<%=ub.getUsername() %>" /></td>
          </tr>
          <tr>
            <td align="right">*真实姓名</td>
            <td><input type="text" name="textfield2" value="<%=ub.getTruename() %>"/></td>
          </tr>
          <tr>
            <td align="right">*家庭住址</td>
            <td><input type="text" name="textfield3" value="<%=ub.getAddress() %>"/></td>
          </tr>
          <tr>
            <td align="right">*联系电话</td>
            <td><input type="text" name="textfield4" value="<%=ub.getPhone() %>"/></td>
          </tr>
          <tr>
            <td align="right">*电子邮件</td>
            <td><input type="text" name="textfield5" value="<%=ub.getEmail() %>"/></td>
          </tr>
          <tr>
            <td align="right">*邮    编 </td>
            <td><input type="text" name="textfield6" value="<%=ub.getPostcode() %>"/></td>
          </tr>
          <tr>
            <td align="right"><input type="submit" name="Submit" value="完成订单" /></td>
            <td><input type="submit" name="Submit2" value="修改个人信息" /></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td align="center"><table width="71%" border="1" cellpadding="0" cellspacing="0" class="abc">
          <tr>
            <td colspan="4" align="center">我的购物车情况</td>
            </tr>
          <tr align="center">
            <td>编号</td>
            <td>商品名称</td>
            <td align="left">单价</td>
            <td align="left">数量</td>
          </tr>
         <%//显示购物车的信息
         for(int i=0;i<al.size();i++){         就是这一行出现空指针,但是al的值已经在上个servlet即login中取得啦,
         GoodsBean gb=(GoodsBean)al.get(i);
         %>
          <tr>
          <td align="center"><%=gb.getGoodsId() %></td>
             <td align="center"><%=gb.getGoodsName() %></td>
             <td><%=gb.getGoodsPrice() %></td>
              <td><%=mcb.getGoodsNumById(gb.getGoodsId()+"") %></td>
              </tr>
         <%
         }
          %>
          <tr align="center">
            <td colspan="4" align="left">您共选择了价值¥<%=mcb.getAllPrice() %>的商品</td>
            </tr>
          <tr align="center">
            <td colspan="4"><input type="submit" name="Submit3" value="修改我的购物车" /></td>
            </tr>
        </table></td>
      </tr>
      <tr>
        <td align="right"><img src="images/shangyibu.gif" width="78" height="30" /> <a href="orderCLServet"><img border="0" src="images/xiayibu.jpg" width="100" height="31" /></a></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td align="center">
     <jsp:include page="tail.jsp"></jsp:include>
    </td>
   </center>
  </body>
</html>

--------------------编程问答-------------------- al.size()  出现的空指针 也就是 al 为null 了、

也就是ArrayList al=(ArrayList)request.getAttribute("mycartinfo"); 这行没有获取到数据

al = null 了、

doGet 方法里、

ArrayList al=mcb.showMyCart(); 这句你debug 看下能获取到null 不?

--------------------编程问答-------------------- 下次建议把报错的语句给贴上来,要不找错误的话得从头看到尾了。
al放入request中了,跳转到shopping3.jsp的时候不知道有没有把al从request中取出来。
我也刚学这个,说的不对的请多指教。 --------------------编程问答-------------------- 我也来看看,这么长的代码啊。 --------------------编程问答--------------------
接2楼的
jsp中
加上这句话<s:debug></s:debug>,
然后看看ValueStack中有木有 --------------------编程问答-------------------- 能把错误代码贴出来?一看就知道哪里空指针了,就知道那个对象没有获取到数据了,这样才是找错误的关键! --------------------编程问答-------------------- //2.把购物车的信息取出
MycartBo mcb=(MycartBo)request.getSession().getAttribute("mycart");
ArrayList al=mcb.showMyCart();
//把al放入request
request.setAttribute("mycartinfo", al); --------------------编程问答-------------------- 我也错在这里了,请问找到原因了么?
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,