MySql DB から User テーブルを表示しようとすると、上記のエラー メッセージが表示されます。ご覧のとおり、次のコードである 33 行目の DisplayUsersServlet によってエラーが生成されます。
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;
}
}
サーブレット クラス
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));
}
}
スタック トレースの出力 (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)