title: Etat HTTP 500 - NullPointerException
body: <p>i'm getting the above error message when trying to display the User table from MySql DB.
  As you can see, the error is generated by the DisplayUsersServlet at line 33 which is the folloxing code : <strong>ArrayList arr = userDb.selectUsers();</strong></p>
  
  <pre><code>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:
  </code></pre>
  
  <p>package business;</p>
  
  <pre><code>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; 
      }
  }
  </code></pre>
  
  <p>Servlet class</p>
  
  <pre><code>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&lt;User&gt; 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);
      }
  }
  </code></pre>
  
  <p>DAO    </p>
  
  <pre><code>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&lt;User&gt; selectUsers() {
  
          ConnectionPool pool = ConnectionPool.getInstance();
          Connection connection = pool.getConnection();
          PreparedStatement ps = null;
          ResultSet rs = null;
  
  
          String query = "select * from User";
          ArrayList&lt;User&gt; arrUsr = new ArrayList&lt;&gt;();
  
          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));
  
      }
  
  }    
  </code></pre>
  
  <p>Print StackTrace (Eclipse console)    </p>
  
  <pre><code>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)
  </code></pre>