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]