简体   繁体   中英

java servlet Null point exception

I keep getting this a java.lang.null pointer exception, any help or suggestions would be much appreciated, I thought by putting in the if statement i would get rid of the errors so i have no idea what is wrong

  StandardWrapperValve[UserManagementServlet]: Servlet.service() for 
  servlet UserManagementServlet threw exception
  java.lang.NullPointerException
      at com.bsapp.servlets.UserManagementServlet.processRequest(UserManagementServlet.java:40)
at com.bsapp.servlets.UserManagementServlet.doGet(UserManagementServlet.java:104)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 

The jsp file is

           </div>
            <form action="<c:url value='/UserManagement?action=listConfirm'/>">
            <!--<input type="text" name="action">-->
            <button class ="btn btn-large" type ="submit">USERS</button>
            </form>
            </div>

and the servlet is

         protected void processRequest(HttpServletRequest request, 
         HttpServletResponse response)
        throws ServletException, IOException {

    String action = request.getParameter("action");

    if (action.equals("list")) {
        doUserListing(request, response);
    }
    if (action.equals("add")) {
        addNewUser(request, response);
    } else{
         out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet SearchServlet</title>");            
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Well Shit</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

void doUserListing(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    UserDAO userDAO = new UserDAO();
    Vector<User> allUsersVect = userDAO.getAllUsers();

    request.getSession(true).setAttribute(IConstants.SESSION_KEY_ALL_USERS, allUsersVect);

    RequestDispatcher rd = request.getRequestDispatcher("/user-listing.jsp");
    rd.forward(request, response);

}

Firstly, you should provide what line is 40. The error is either that request or action is null as far as I can tell with the limited information you provided. Try adding

if (action == null)
    return;

Make sure to replace return; with any error processing you need to execute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM