尝试从MySql DB显示User表时,出现上述错误消息。
如您所见,该错误是由DisplayUsersServlet在第33行生成的,它是以下代码:ArrayList arr = userDb.selectUsers();
java.lang.NullPointerException
user.DisplayUsersServlet.doGet(DisplayUsersServlet.java:33)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
Here are my classes:
包装业务;
import java.io.Serializable;
public class User implements Serializable
{
private String firstName;
private String lastName;
private String emailAddress;
public User()
{
firstName = "";
lastName = "";
emailAddress = "";
}
public User(String first, String last, String email)
{
firstName = first;
lastName = last;
emailAddress = email;
}
public void setFirstName(String f)
{
firstName = f;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String l)
{
lastName = l;
}
public String getLastName()
{
return lastName;
}
public void setEmailAddress(String e)
{
emailAddress = e;
}
public String getEmailAddress()
{
return emailAddress;
}
}
Servlet类
package user;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.ArrayList;
import business.User;
import data.UserDB;
public class DisplayUsersServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
UserDB userDb;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO: get an ArrayList of User objects from the database
ArrayList<User> arr = userDb.selectUsers();
HttpSession session = request.getSession();
// TODO: set the ArrayList of User objects as a session attribute
session.setAttribute("users", arr);
String url = "/users.jsp";
RequestDispatcher dispatcher = getServletContext()
.getRequestDispatcher(url);
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
道
package data;
import java.sql.*;
import java.util.ArrayList;
import business.User;
public class UserDB
{
// TODO: add code that returns an ArrayList of User objects
// that corresponds with the rows in the User table
public ArrayList<User> selectUsers() {
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String query = "select * from User";
ArrayList<User> arrUsr = new ArrayList<>();
try {
ps = connection.prepareStatement(query);
rs = ps.executeQuery();
User user = null;
while (rs.next()) {
user = resultSetRowToUsers(rs);
if (user.getFirstName()!=null)
arrUsr.add(resultSetRowToUsers(rs));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return arrUsr;
}
public User resultSetRowToUsers(ResultSet resultSet) throws SQLException {
//User user = new User();
return new User(resultSet.getString(1), resultSet.getString(2),
resultSet.getString(3));
}
}
打印StackTrace(Eclipse控制台)
Jul 11, 2013 2:12:53 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: "Servlet.service()" for servlet DisplayUsersServlet has generated an exception
java.lang.NullPointerException
at user.DisplayUsersServlet.doGet(DisplayUsersServlet.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
最佳答案
是否需要初始化userDb?