异常是:为什么说我是空指针异常呢??(我的数据库里是有数据的)
org.apache.jasper.JasperException: Exception in JSP: /index.jsp:213210:     <th align=center bgcolor="#FFFFFF" ><span class="STYLE8"><strong>单价</strong></span></th>
211:     <th align=center bgcolor="#FFFFFF" ><span class="STYLE8"><strong>购物车</strong></span></th>
212:    <%Vector booklist=(Vector)session.getAttribute("booklist");
213:     while(booklist.size()>=0){
214:         int i=0;
215:       BookInformation book=(BookInformation)booklist.elementAt(i);
216: 
Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.lang.NullPointerException
    org.apache.jsp.index_jsp._jspService(index_jsp.java:267)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.

解决方案 »

  1.   

    从你的代码和逻辑上看,应该不会出java.lang.NullPointerException异常。你的异常应该是从页面上看到的,
    但是,tomcat常常都会把真正的异常掩盖了,
    而只报最后的异常。也就是说,
    你所看到的java.lang.NullPointerException异常
    不一定是程序出错的原因。你可以去tomcat目录下的log里去找找
    刚刚记录的日志文件
    那里面会记录非常详细的异常链信息
    应该会对你有帮助的。
      

  2.   

    是Tomcat下的logs文件夹吗??
    可是我的logs下的每个文件打开都是空的啊!!
      

  3.   

    应该不会这样的!
    比如说localhost_log_2007-01-15.txt
    ..err、..out之类的文件。如果确实没有
    你再看一下你的应用项目的配置文件
    看看配置文件中把log文件记录在什么位置了:)
      

  4.   

    我前几天就试过在LINUX下TOMCAT5的SESSION信息总是无效,,明明是SESSION,但是传到页面总是NULL,,把同样代码在WINWODS下测试却正常。
      

  5.   

    不知道楼主的这环境有没有测过SESSION?
      

  6.   

    int i=0;
    要放在while循环外
      

  7.   

    while(booklist.size()>=0)在这一行报NullPointerException,很明显,这里只有booklist可能为nullVector booklist=(Vector)session.getAttribute("booklist");
    如果session里不存在名为booklist的属性,上面这一句就会返回null因此,可能是你的session里没有booklist存在,
    你可以在while之前用
    System.out.println(booklist);
    打印一下,从控制台看看是不是null从你前面的程序来看,这里不应该是null的。
    为什么session.setAttribute()没起作用?难道是设置了之后其它地方又删除掉了?
      

  8.   

    按理说不会是你的servlet出错,
    否则,就会编译不过去。所以,应该是你的jsp页面出了错。
    你如果找不到log,那么只能做如下推理:1、在你的“显示主业信息代码”(Vector booklist=(Vector)session.getAttribute("booklist");
    ...)之前应该还有其他java代码片断;
    2、应该就是之前的java代码片断出了错,但是,被NullPointerException掩盖了;
    3、你最好把这些代码也贴出来,包括引入包和bean的代码:)
      

  9.   

    这是Servlet
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.ResultSet;
    import java.util.Vector;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 bean.BookInformation;
    import bean.Database;
    public class showbook extends HttpServlet {
    public showbook() {
    super();
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
           request.setCharacterEncoding("gb2312");
           String key=request.getParameter("key");
           HttpSession session=request.getSession();
           Vector booklist=new Vector();
           String sql="select * from book where";
           String type=request.getParameter("type"); 
           if(session.getAttribute("booklist")!=null){
            session.removeAttribute("booklist");
           }
           if(key==null){
            if(type==null){
            sql+="ifNew='"+"yes"+"'";
            }
            else{
            if(type.equals("computer")){sql+="type='"+"计算机"+"'";}
            if(type.equals("music")){sql+="type='"+"音乐"+"''";}
            if(type.equals("sport")){sql+="type='"+"体育"+"''";}
            if(type.equals("art")){sql+="type='"+"美术"+"''";}
            if(type.equals("ecnomy")){sql+="type='"+"经济"+"''";}
            if(type.equals("travel")){sql+="type='"+"旅游"+"''";}
            }
           
           }
           else{
            sql+="name like"+"\'%"+key+"%\'"+
            "or author like"+"\'%"+key+"%\'";
            
           }
       
           Database db=new Database();
           db.connection();
           ResultSet rs=db.executeQuery(sql);
           try{
           while(rs.next()){
            BookInformation bookinf=new BookInformation(rs.getString("id"),
                 rs.getString("name"),
                 rs.getString("author"),
                 rs.getString("publisher"),
                 rs.getString("price"),
                 rs.getString("type"));
            booklist.addElement(bookinf);
           }
           }catch(Exception e){
            
           }
    db.close();
     session.setAttribute("booklist", booklist);
     response.sendRedirect("/bookshoponline/index.jsp");
    } public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    }}
      

  10.   

    这是BEAN的代码:
    package bean;public class BookInformation {
        private String id;
        private String name;
        private String author;
        private String publisher;
        private String price;
        private String type;
        public BookInformation(){
         id="";
         name="";
         author="";
         publisher="";
         price="";
         type="";
        }
    public BookInformation(String id, String name, String author, String type, String price, String string6) {
    this.id=id;
    this.name=name;
    this.author=author;
    this.publisher=publisher;
    this.price=price;
    this.type=type;
    }
    public String getId(){
    return id;
    }
    public void setId(String id){
    this.id=id;
    }
    public String getName(){
    return name;
    }
    public void setName(String name){
    this.name=name;
    }
    public String getAuthor(){
    return author;
    }
        public void setAuthor(String author){
         this.author=author;
        }
        public String getPublisher(){
         return publisher;
        }
        public void setPublisher(String publisher){
         this.publisher=publisher;
        }
        public String getPrice(){
         return price;
        }
        public void setPrice(String price){
         this.price=price;
        }
        public String getType(){
         return type;
        }
        public void setType(String type){
         this.type=type;
        }
    }
      

  11.   

    这是index.jsp中带JSP程序的代码:<% request.setCharacterEncoding("gb2312");
    if(session.getAttribute("booklist")==null){
    response.sendRedirect("/bookshoponline/showbook");
    }
    %>
    //.....
     <% Vector booklist=(Vector)session.getAttribute("booklist");
         System.out.print(booklist);
       //for(Iterator index=booklist.iterator();index.hasNext();){
       //BookInformation book=(BookInformation)index.next();
          %> 
        <tr>
        <td align=center>JAVA基础<%//=book.getName()%></td>
    <td align=center>胖子<%//=book.getAuthor() %></td>
        <td align=center>胖子工作室<%//=book.getPublisher()%></td>
        <td align=center>计算机<%//=book.getType()%></td>
        <td align=center>100<%//=book.getPrice()%>元</td>
        <%//}%>
      

  12.   

    你的sql语句是有问题的!
    你把sql打出来看看~
      

  13.   

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            ......        String sql = "select * from book where 1=1 ";
            String key = request.getParameter("key");
            String type = request.getParameter("type");        if (key != null && !key.equals("")) {
                sql += " and name like '%" + key + "%' or author like '%"
                    + key + "%'";
            }        String sqlType = getSqlType(type);
            if ("".equals(sqlType)) {
                sql += " and ifNew='yes' ";
            }
            else {
                sql += " and type='" + sqlType + "'";
            }        ......
        }    public String getSqlType(String type) {        if (type == null || type.equals("")) {
                return "";
            }        String sqlType = "";
            if (type.equals("computer")) {
                sqlType = "计算机";
            }
            else if (type.equals("music")) {
                sqlType = "音乐";
            }
            else if (type.equals("sport")) {
                sqlType = "体育";
            }
            else if (type.equals("art")) {
                sqlType = "美术";
            }
            else if (type.equals("ecnomy")) {
                sqlType = "经济";
            }
            else if (type.equals("travel")) {
                sqlType = "旅游";
            }        return sqlType;
        }
      

  14.   

    晕了!!我照你说的改了改:
    这个SERVLET:
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
           request.setCharacterEncoding("gb2312");
           String key=request.getParameter("key");
           HttpSession session=request.getSession();
           Vector booklist=new Vector();
           String sql="select * from book where 1=1";
           String type=request.getParameter("type"); 
           if(session.getAttribute("booklist")!=null){
            session.removeAttribute("booklist");
           }
           if (key != null && !key.equals("")) {
               sql += " and name like '%" + key + "%' or author like '%"
                   + key + "%'";
           }       String sqlType = getSqlType(type);
           if ("".equals(sqlType)) {
               sql += " and ifNew='yes' ";
           }
           else {
               sql += " and type='" + sqlType + "'";
           }
           Database db=new Database();
           db.connection();
           ResultSet rs=db.executeQuery(sql);
          try{
    while(rs.next()){
    BookInformation book=new BookInformation(rs.getString("id"),rs.getString("name"),rs.getString("author"),rs.getString("publisher"),rs.getString("price"),rs.getString("type"));  
    booklist.addElement(book);
    }
           }catch(Exception e){
            
           }
    db.close();
     session.setAttribute("booklist", booklist);
     response.sendRedirect("/bookshoponline/index.jsp");
    } public String getSqlType(String type) {        if (type == null || type.equals("")) {
                return "";
            }        String sqlType = "";
            if (type.equals("computer")) {
                sqlType = "计算机";
            }
            else if (type.equals("music")) {
                sqlType = "音乐";
            }
            else if (type.equals("sport")) {
                sqlType = "体育";
            }
            else if (type.equals("art")) {
                sqlType = "美术";
            }
            else if (type.equals("ecnomy")) {
                sqlType = "经济";
            }
            else if (type.equals("travel")) {
                sqlType = "旅游";
            }        return sqlType;
        }
      

  15.   

    这是index.jsp
    <% Vector booklist=(Vector)session.getAttribute("booklist");
          Iterator index=booklist.iterator();
          while(index.hasNext()){
     BookInformation book=(BookInformation)index.next();
         %>
        <tr>
        <td align=center>JAVA基础<%=book.getName()%></td>
    <td align=center>耗子<%=book.getAuthor()%></td>
        <td align=center>胖子工作室<%=book.getPublisher()%></td>
        <td align=center>计算机<%=book.getType()%></td>
        <td align=center>100<%=book.getPrice()%>元</td>
        <%if(session.getAttribute("userid")!=null){%>
        <td align=center><a href="/bookshoponline/addCart.jsp");">购买</a></td>
        <%
        }else{%><td align=center>登陆后可以购买</td><%}%>
        </tr>
        <%
        }%>
    但是还小相同的异常,基本上可以确定是因为booklist==null吗??
    大哥再帮我看看这是为什么啊??
      

  16.   

    你单步调试一下吧。并且异常处理的方法不要空着,一旦有异常,也不会知道。
    }catch(Exception e){
       //处理...     
    }