ResultSet列检索中的JDBC NullPointerException

时间:2015-05-05 16:51:24

标签: java mysql eclipse jdbc

这是我的代码,旨在从数据库中获取信息并将其显示到JSP页面。我似乎无法看到错误是什么,因为所有字段都已正确声明,并且当在if else块中获取参数时,它可以工作,但除此之外,它不会。我使用MySQL和Eclipse。

package com.information.process;
import com.information.personal.PersonalBean;

import java.sql.*;
import java.util.ArrayList;

import com.information.personal.UserDonationBean;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ReturnInfo
 */
@WebServlet("/informationReturn")
public class ReturnInfo extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private Connection con = new DBConnection().connect();
    private ArrayList<UserDonationBean> u;
    private ArrayList<PersonalBean> p;
    private String username;
    private String amount;
    private String toDate;
    private String fromDate;
    private ResultSet rs;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try
        {
            HttpSession session = request.getSession(false);
            username = request.getParameter("username");
            amount = request.getParameter("amount");
            toDate = request.getParameter("toDate");
            fromDate = request.getParameter("fromDate");

            getLog();
            session.setAttribute("query", u);
            session.setAttribute("names", p);
            response.sendRedirect(request.getContextPath() + "/admin-content-wp/donation_log_wp.jsp");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    protected void getLog() throws SQLException
    {
        Statement st = con.createStatement();
        ResultSet rs2;
        if (username.equals("") && amount.equals(""))
        {
            rs = st.executeQuery("SELECT * FROM DonationLog WHERE DateDonated BETWEEN '"+fromDate+"' AND '" + toDate+"'");
            //rs.next(); System.out.println(rs.getInt("DonationID")); //this line does not generate a null pointer exception but the while loop does
        }
        else if (amount.equals(""))
        {
            rs = st.executeQuery("SELECT * FROM DonationLog WHERE Username = '"+username+"' AND DateDonated BETWEEN '"+fromDate+"' AND '"+toDate+"'");
        }
        else if (username.equals("") )
        {
            rs = st.executeQuery("SELECT * FROM DonationLog WHERE Amount = "+amount+" AND DateDonated BETWEEN '"+fromDate+"' AND '"+toDate+"'");
        }
        else
        {
            rs = st.executeQuery("SELECT * FROM DonationLog WHERE Username = '" + username + "' AND Amount = '" + amount + "' AND DateDonated BETWEEN '"+fromDate+"' AND '"+toDate+"'");
        }

        u = new ArrayList<UserDonationBean>();
        p = new ArrayList<PersonalBean>();

        while(rs.next()) // where the error occurs
        {
            PersonalBean pb = new PersonalBean();
            UserDonationBean ud = new UserDonationBean();
            rs2 = st.executeQuery("SELECT Lastname, Firstname FROM PersonalInformation WHERE Username = '" + rs.getString("Username") + "'");
            rs2.next();
            pb.setLastName(rs2.getString("Lastname"));
            pb.setFirstName(rs2.getString("Firstname"));
            ud.setDonationID(rs.getInt("DonationID"));
            ud.setAmount(rs.getDouble("Amount"));
            ud.setDateDonated(rs.getDate("DateDonated"));
            ud.setUsername(rs.getString("Username"));
            u.add(ud);
            p.add(pb);
        }
    }
}

这是问题的堆栈跟踪:

SEVERE: Servlet.service() for servlet [com.information.process.ReturnInfo] in context with path [/FoundationSystem] threw exception
java.lang.NullPointerException
    at com.mysql.jdbc.ResultSet.findColumn(ResultSet.java:966)
    at com.mysql.jdbc.ResultSet.getInt(ResultSet.java:2749)
    at com.information.process.ReturnInfo.getLog(ReturnInfo.java:90)
    at com.information.process.ReturnInfo.doPost(ReturnInfo.java:46)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:2)

请勿使用相同的连接。在获得第二个rs2之前,从第一个rs中获取所有值。

在下面的更正代码中,我首先使用rs在重新使用连接(通过st对象)之前从中提取我想要的值以获取rs2。

    while(rs.next()) // where the error occurs
    {
        PersonalBean pb = new PersonalBean();
        UserDonationBean ud = new UserDonationBean();
        ud.setDonationID(rs.getInt("DonationID"));
        ud.setAmount(rs.getDouble("Amount"));
        ud.setDateDonated(rs.getDate("DateDonated"));
        ud.setUsername(rs.getString("Username"));
        rs2 = st.executeQuery("SELECT Lastname, Firstname FROM PersonalInformation WHERE Username = '" + rs.getString("Username") + "'");
        rs2.next();
        pb.setLastName(rs2.getString("Lastname"));
        pb.setFirstName(rs2.getString("Firstname"));
        u.add(ud);
        p.add(pb);
    }
}