這是我的代碼,旨在從數據庫獲取信息並將其顯示到JSP頁面。我似乎無法看到錯誤是什麼,因爲所有字段都已正確聲明,並且在if else塊中獲取參數時,它可以正常工作,但在其之外,它不會。我使用MySQL和Eclipse。ResultSet列檢索中的JDBC NullPointerException
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)
表DonationLog的模式是什麼? –
@MoH。你確定嗎?堆棧跟蹤在「at com.mysql.jdbc.ResultSet.getInt」處聲明它的位置。我寧願相信痕跡的方法名稱,然後假定行數與編譯的內容匹配。 –
爲什麼所有的downvotes? –