java servlet空点异常

时间:2019-01-10 18:52:02

标签: java servlets

我一直在得到这个java.lang.null指针异常,我非常感谢任何帮助或建议,我认为通过放入if语句可以摆脱错误,所以我不知道哪里出了问题

  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) 

jsp文件是

           </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>

而servlet是

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

}

1 个答案:

答案 0 :(得分:2)

首先,您应该提供40行。 据我所提供的有限信息所知,错误是requestaction为空。尝试添加

if (action == null)
    return;

请确保将return;替换为您需要执行的任何错误处理。