此电脑 2009-02-27 13:12
浏览 282
已采纳

一个空指针异常

jsp+servlet+javaBean做的个新闻发布
但是老是报一个空指针异常
数据库内有数据

业务:[code="java"] public LinkedList list() {
String sql = "select * from news order by id desc limit 0,5";
LinkedList list = new LinkedList();
MySQLConnection conn = ConnectionPool.getInstance().getConnection();
ResultSet rs = conn.Query(sql);
System.out.println("this....................");
try {
while (rs.next()) {
News news = new News(rs.getInt(1), rs.getString(2), rs
.getString(3), rs.getString(4), rs.getString(5));
list.add(news);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}[/code]

Servlet:
[code="java"] public void list(HttpServletRequest req, HttpServletResponse res) {
Bnews b = new Bnews();
List list = b.list();
req.setAttribute("list", list);
try {
res.sendRedirect("/jsp/index.jsp");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}[/code]

页面:
[code="java"]


<%
List list = (List) request.getAttribute("list");
                        News news = null;

                        for(int i = 1;i<=list.size();i++)
                        { 
                            news = (News)list.get(i);
                            System.out.println(news.getTitle()+"tttttttt");
                        %>
      <div class="newsinf">
        <div class="inftitle"> <a href="#"><%=news.getTitle()%></a> </div>

[/code]

异常:
[code="java"]type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Exception in JSP: /jsp/index.jsp:104

101: List list = (List) request.getAttribute("list");
102:

103: News news = null;
104: System.out.println(news.getTitle()+"tttttttt");
105: for(int i = 1;i<=list.size();i++)
106: {
107: news = (News)list.get(i);

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.jsp.index_jsp._jspService(index_jsp.java:147)
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)

[/code]

[b]问题补充:[/b]
System.out.println(news.getTitle()+"tttttttt");

删除
或者放在
news = (News)list.get(i);

再者 news = (News)list.get(i); 已经实例化

Exception:
[code="java"]org.apache.jasper.JasperException: Exception in JSP: /jsp/index.jsp:105

102:

103: News news = new News();
104:

105: for(int i = 1;i<=list.size();i++)
106: {
107: news = (News)list.get(i);
108: System.out.println(news.getTitle()+"tttttttt");

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.jsp.index_jsp._jspService(index_jsp.java:148)
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)

[/code]

  • 写回答

5条回答 默认 最新

  • iteye_87 2009-02-27 15:44
    关注

    org.apache.jasper.JasperException: Exception in JSP: /jsp/index.jsp:105

    101: List list = (List)request.getAttribute("list");

    102:

    103: News news = new News();

    104:

    105: for(int i = 1;i<=list.size();i++)

    106: {

    107: news = (News)list.get(i);

    108: System.out.println(news.getTitle()+"tttttttt");

    list你没有得到,为null,所以105循环时报java.lang.NullPointerException
    另外循环是从i=0开始,list.size()-1结束

    至于list=null的问题,是由于你用的是重定向,request里保存的值并没有传过到页面上,用转发就好了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 有偿,谁有移远的EC200S固件和最新的Qflsh工具。
  • ¥15 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错