HTTP状态500 - 在第104行处理JSP页面/header.jsp时发生异常

时间:2017-11-26 05:28:17

标签: java jsp connection

我收到以下错误:

  

org.apache.jasper.JasperException:处理发生异常   第104行的JSP页面/header.jsp

 <%if (categoryDAO.getListCategory() != null) {
      for (Category ds : categoryDAO.getListCategory()) {%>
<li><a href="products.jsp?category=<%=ds.getCategoryID()%>"> <%=ds.getCategoryName()%></a></li>

堆栈跟踪:

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

根本原因

java.lang.NullPointerException
org.apache.jsp.header_jsp._jspService(header_jsp.java:235)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

有我的categoryDAO

  public ArrayList<Category> getListCategory() throws SQLException, ClassNotFoundException {
    Connection cons = DBconnect.getConnecttion();
    String sql = "SELECT * FROM category";
    ArrayList<Category> list = new ArrayList<>();
    PreparedStatement ps = null;//cai
    try {
        try {
            ps = (PreparedStatement) cons.prepareStatement(sql);
        } catch (Exception e) {

            return null;
        }
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            Category category = new Category();
            category.setCategoryID(rs.getLong("category_id"));
            category.setCategoryName(rs.getString("category_name"));
            list.add(category);
        }
        cons.close();
    } catch (SQLException e) {
         cons.close();
        return null;

    }
    return list;
}

这是我的数据库连接

 static final String DBNAME = "shop";
static final String USERNAME = "root";
static final String PASSWORD = "vancao";
private static ResultSet rs = null;
private static Connection connection = null;

public static Connection getConnecttion() throws ClassNotFoundException, SQLException {
    if (connection != null) {
        return connection;
    } else {
        Class.forName("com.mysql.jdbc.Driver");
        String connectionURL = "jdbc:mysql://" + HOSTNAME + ":3306/" + DBNAME + "?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
        connection = DriverManager.getConnection(connectionURL, USERNAME,
                PASSWORD);
        return connection;
    }
}

我检查了CategoryDAO中的getListCategory(),它仍然有效。 我的代码会发生什么?请帮我。谢谢大家。

1 个答案:

答案 0 :(得分:0)

if (categoryDAO.getListCategory() != null) {
          for (Category ds : categoryDAO.getListCategory()) {
  out.write("\n");
  out.write("<li><a href=\"products.jsp?category=");
  out.print(ds.getCategoryID());
  out.write(" \"> ");
  out.print(ds.getCategoryName());
  out.write("</a></li>\n");
  out.write("\n");
  out.write("                                    ");

第235行是:for(类别ds:categoryDAO.getListCategory()) 有问题? Ramanlfc。