0

我在尝试从 MySql DB 显示用户表时收到上述错误消息。如您所见,错误是由第 33 行的 DisplayUsersServlet 生成的,它是以下代码: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; 
    }
}

小服务程序类

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)
4

3 回答 3

2

userDb 需要初始化吗?

于 2013-07-11T12:39:25.757 回答
1

您的userDb实例变量是null. 它没有被实例化也没有被注入,所以它会保持它的默认值null.

要么自己实例化它,要么(如果您使用 Servlet 规范 3)将其注入。

于 2013-07-11T12:39:33.900 回答
1

您应该在代码中插入注释,指出第 33 行的位置。但是,我猜这个userDb变量永远不会被初始化。你没有使用任何形式的依赖注入,也没有分配给它,所以它保持为空。

顺便说一句,GET 和 POST 是不同的东西。不要一个电话另一个。

于 2013-07-11T12:39:47.943 回答