servlet相关代码: request.getSession().setAttribute("totalPage", totalPage);
在jsp页: String totalPage = (String)session.getAttribute("totalPage");
 int total = Integer.parseInt(totalPage);异常信息:--------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: null
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Integer.java:415)
java.lang.Integer.parseInt(Integer.java:497)
org.apache.jsp.index_jsp._jspService(index_jsp.java:49)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
为什么会有数据转换错误,
--------------------------------------------------------------------------------Apache Tomcat/5.5.25

解决方案 »

  1.   

    UP?!写J2ee时,jsp里的html用什么写,手工写很烦也容易出錯,或都这样说jsp页代码用什么写 
      

  2.   

    你的代码是没什么问题的  是不是totalPage里面存的数据错了?
      

  3.   

    把完整的代码贴出来:jsp代码:<%@ page language="java" pageEncoding="gb2312"%>
    <%@ page import="java.sql.ResultSet ,java.sql.Statement" %>
    <% 
     ResultSet rs = (ResultSet)session.getAttribute("rs");
      Object totalPage = session.getAttribute("totalPage");
     int total = Integer.parseInt(totalPage.toString());
     
     Object currentPage = session.getAttribute("totalPage");
     int current = Integer.parseInt(currentPage.toString());
     %><html>
    <head>
    <title>My JSP 'index.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">
    </head> <body>
     <table withd="500"  height="300" align="center" border="1">
    <tr withd="500"><th >stor_id</th><th>ord_num</th> 
    <th>ord_date</th> <th>qty</th> 
    <th>payments</th> <th>title_id</th> 
       
    </tr>
    <%
       if(rs!=null){
       while(rs.next()){
       
     %>
         <tr width="500"><td><%=rs.getString("stor_id") %></td><td><%=rs.getString("stor_id") %></td><td> <%=rs.getString("ord_date") %></td><td><%=rs.getString("qty") %></td><td>%=rs.getString("payments") %></td><td><%=rs.getString("title_id") %></td></tr>
         <%} %>
         <tr><td>  <a href="/list/listServer?curretnPage=1"> 首页</a></td><td><a href="/list/listServer">下一页</a></td></tr>
              <tr><td>  <a href="/list/listServer"> 上一页</a></td><td><a href="/list/listServer">尾页</a></td></tr>
              
              </table>
              
              <%} else {%>
              <font color="red" size="10">
              <%
                 out.println("没有你要找页面!");
               %><%
               }
                %>
                </font>
                <div align="center">
                  <a href="/list/listServer?currentPage=1"> 首页</a>共有<%=total %>&nbsp;当前是第<%=current %>页<a href="/list/listServer">下一页</a>
                  <a href="/list/listServer"> 上一页</a>&nbsp;&nbsp;&nbsp;<a href="/list/listServer">尾页</a>
              </div>
    </body>
    </html>Server代码:package com.fuyou;import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.ResultSet;
    import java.sql.SQLException;import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class listServer extends HttpServlet {
    private DBConnection dbc = null; private int currentPage = 0; private int eachPage = 5; private int totalPage = 0; public void destroy() {
    super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { doPost(request, response);
    } public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    int total = 0;
    String sql = "select stor_id , ord_num,  ord_date ,qty ,payterms,title_id from sales ";
    ResultSet rs = dbc.executeQuery(sql);
    if (request.getParameter("currentPage") != null) {
    this.currentPage = Integer.parseInt(request
    .getParameter("currentPage"));
    } if (rs != null) {


    try {
    rs.last();
    total = rs.getRow();
    totalPage = (total + eachPage - 1) / eachPage;
    System.out.println(totalPage);
    request.getSession().setAttribute("totalPage", Integer.valueOf(totalPage));
    rs.beforeFirst();
    while (rs.next()) {

    request.getSession().setAttribute("rs", rs);
    // System.out.print(rs.getString("stor_id")); System.out.print(rs.getString("ord_num")); // System.out.print(rs.getTimestamp("ord_date"));
    // System.out.print(rs.getShort("qty"));
    // System.out.print(rs.getString("payterms"));
    // System.out.print(rs.getString("title_id"));*/ total = rs.getRow();
    System.out.println(total); }
    } catch (SQLException e) { e.printStackTrace();
    }
    } else {
    System.out.println("rs==111null");
    }
    if (this.currentPage < 1 || this.currentPage == 0) {
    this.currentPage = 1;
    }
    request.getSession().setAttribute("currentPage", Integer.valueOf(currentPage));
    // System.out.println(totalPage);
    System.out.println(this.currentPage);
    RequestDispatcher requestDipathcer = request
    .getRequestDispatcher("/index.jsp");
    requestDipathcer.forward(request, response);
    } public void init() throws ServletException {
    this.dbc = new DBConnection();
    }}上面两运行有下面错误:HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Exception in JSP: /index.jsp:63: <% 
    4:  ResultSet rs = (ResultSet)session.getAttribute("rs");
    5:   Object totalPage = session.getAttribute("totalPage");
    6:  int total = Integer.parseInt(totalPage.toString());
    7:  
    8:  Object currentPage = session.getAttribute("totalPage");
    9:  int current = Integer.parseInt(currentPage.toString());
    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    root cause java.lang.NullPointerException
    org.apache.jsp.index_jsp._jspService(index_jsp.java:49)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    note The full stack trace of the root cause is available in the Apache Tomcat/5.5.25 logs.
      

  4.   

    你的Connection、PreparedStatement、ResultSet都关掉了吗?既然用了Servlet,就应该按照MVC的要求来做,不要在页面上操纵ResultSet。private int currentPage = 0;
    private int eachPage = 5;
    private int totalPage = 0;把这些作为局部变量,没有必要弄成成员变量的。
    如果是首次执行index.jsp的话,Session里面肯定是空的了啦。
      

  5.   

    没关啊,问题关键是数据转换出錯了,我把变里存在session里,在jsp里取出来用!
      

  6.   

    源码:
    String totalPage = (String)session.getAttribute("totalPage");
    int total = Integer.parseInt(totalPage);
    这样的写法无法排除totalPage=null是字符转换出错。
    使用Integer.parseInt这个函数时要保证totalPage是数字。
    可以改为如下代码:
    String totalPage = (String)session.getAttribute("totalPage");
    if(totalPage.equals("")||totalPage==null)
    totalPage="0";
    int total = Integer.parseInt(totalPage);
    保证转换时非null和非空,就不会出现异常了。
      

  7.   

    LZ,出错信息报的空指针啊!
    看看这句int total = Integer.parseInt(totalPage.toString());
    totalPage.toString(),如果totalPage是null就出错啦!这种潜在的异常应该要处理一下的啊!
      

  8.   

    ResultSet rs = dbc.executeQuery(sql);执行后可能查不到数据,rs为空了,所以也没有进到if (rs != null) {
    }循环体里,Object totalPage = session.getAttribute("totalPage");
    返回的也就是空对象null.