HTTP状态500 - java.lang.NullPointerException

时间:2015-03-19 08:29:57

标签: java mysql jsp exception

我是java的新手,jsp我正在尝试使用index.jsp从Mysql数据库中打印一些数据。正如标题所说我得到了例外。我已经检查过这个例外了很多。我在Stackoverflow中看到了一些相同的帖子,但是他们没有帮助我。这是我的代码:

index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action ="New_printer.jsp" method="get">
    <input type ="submit" value="Proceed">
</form>

</body>
</html>

New_printer.jsp:

<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<%@ page import="com.results.Printer" %>

<%
int i;
ArrayList Rows = new ArrayList();

Rows = (ArrayList) request.getAttribute("data");

for(i=1; i<Rows.size(); i++)
{
    ArrayList row = new ArrayList();

    row = (ArrayList) Rows.get(i);
    for(int j=1; j<4; j++)
    {
        String word =(String) row.get(j);
        out.print(word + " ");
    }
    out.println();  
}

%>

</body>
</html>

Printer.java:

package com.results;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.io.IOException;

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 Printer extends HttpServlet{

    private static Connection con = null;
    ResultSet rs;



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        resp.setContentType("text/jsp");

        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/tst","root","1234");
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery("select * from test_db.person");

            ArrayList Rows = new ArrayList();

            while(rs.next())
            {
                ArrayList row =new ArrayList();
                for(int i=1; i<4; i++)
                {
                    row.add(rs.getString(i));
                }
                Rows.add(row);
            }

            req.setAttribute("data", Rows);
            RequestDispatcher rqdp = getServletContext().getRequestDispatcher("/New_printer.jsp");
            rqdp.forward(req, resp);

        }

        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Simple_Print</display-name>
  <!-- 
  <servlet>
    <servlet-name>printer</servlet-name>
    <servlet-class>com.results.Printer</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>printer</servlet-name>
    <url-pattern>/index.jsp</url-pattern>
  </servlet-mapping> -->

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

异常

HTTP Status 500 - java.lang.NullPointerException

type Exception report

message java.lang.NullPointerException

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

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:548)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:469)
    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:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.NullPointerException
    org.apache.jsp.New_005fprinter_jsp._jspService(New_005fprinter_jsp.java:123)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:431)
    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:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note The full stack trace of the root cause is available in the Apache Tomcat/8.0.20 logs.
你能帮忙吗?请我在这里堆叠好几天。

1 个答案:

答案 0 :(得分:0)

问题是您要将表单从index.jsp提交到New_printer.jsp,但您尚未通过data属性,因此request.getAttribute("data");会为您提供空指针。< / p>

你在做什么:

index.jsp - - - - &GT; New_printer.jsp

你应该做什么:

index.jsp - - - - &GT; Printer.java -------&GT; New_printer.jsp