Etat HTTP 500 - NullPointerException

时间:2013-07-11 12:35:25

标签: java mysql servlets nullpointerexception

我在尝试从MySql DB显示User表时收到上述错误消息。 正如您所看到的,错误是由第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; 
    }
}

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);
    }
}

DAO

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

    }

}    

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

3 个答案:

答案 0 :(得分:2)

userDb需要初始化吗?

答案 1 :(得分:1)

您的userDb实例变量为null。它没有实例化也没有注入,所以它将保持默认值null

自己实例化,或者(如果你使用Servlet规范3)注入它。

答案 2 :(得分:1)

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

顺便说一句,GET和POST是不同的东西。没有人打电话给对方。