【问题标题】:Null pointer exception on click submit单击提交时出现空指针异常
【发布时间】:2017-03-16 14:03:35
【问题描述】:

我使用 Java 和 sql 创建了一个用户管理(创建、读取、更新、删除)。 这是我的道:

private static final String UPDATE = "UPDATE user set login_user=?, pwd_user=?, id_role=? where id_user=? ";

@Override
public void update_user(User user) throws DAOException {
    Connection connexion = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;

    try {
        connexion = (Connection) dao_factory.getConnection();
        preparedStatement = connexion.prepareStatement(UPDATE);  
        preparedStatement.setString(1, user.getLogin());
        preparedStatement.setString(2, user.getPwd());
        preparedStatement.setInt(3, user.getRole());
        preparedStatement.setLong(4, user.getId_user());
        preparedStatement.executeUpdate();


    } catch (SQLException e) {
        throw new DAOException(e);
    } 

}

我的更新用户的jsp:

<%
if(action.equalsIgnoreCase("edit")){
%>
<form method="get" action="client">
   <div class="form-group">
        <input type="text" class="form-control" id="login" name="login" placeholder="login">
        <a href="client?action=update&id=${ user_edit.id_user }" class="btn btn-primary">Update</a>
   </div>
 </form>
 <% } %>

还有我的 servlet:

String action = request.getParameter("action");
    if (request.getParameter("action") != null && action.equalsIgnoreCase("view")) {
        request.setAttribute("client", user_dao.find_all());
    }
    if (action.equalsIgnoreCase("delete")) {
        int id = Integer.parseInt(request.getParameter("id"));
        user_dao.delete_user(id);
        request.setAttribute("client", user_dao.find_all());
    }
    if (action.equalsIgnoreCase("edit")) {
        int id = Integer.parseInt(request.getParameter("id"));
        User user = user_dao.getById(id);
        session.setAttribute("user_edit", user);
    } 
    if (action.equalsIgnoreCase("update")) {
        Long id = Long.parseLong(request.getParameter("id"));
        String login = request.getParameter("login");
        User user = new User();
        user.setLogin("test_login");
        user.setPwd("test_pwd");
        user.setRole(1);
        user.setId_user(id);
        user_dao.update_user(user);
    }
    else {
        request.setAttribute("client", user_dao.find_all());
    }

    this.getServletContext().getRequestDispatcher(VUE).forward(request, response);

我的第一个问题是,当我单击“更新”按钮时,它可以工作。但是如果我按回车,我有一个空指针异常

第二个问题是,如果我想用String login = request.getParameter("login");user.setLogin(login);恢复登录用户,则db中的登录值为空。

非常感谢

编辑: 这是堆栈跟踪:

Avertissement:   StandardWrapperValve[client]: Servlet.service() for servlet `client threw exception`
java.lang.NullPointerException
at egame.servlets.admin.client.processRequest(client.java:53)
at egame.servlets.admin.client.doGet(client.java:94)
  • 第 53 行:if (action.equalsIgnoreCase("delete"))
  • 第 94 行为空

【问题讨论】:

  • 堆栈跟踪是什么?你做了什么来调试它?
  • a 添加了堆栈。谢谢

标签: java jsp servlets input nullpointerexception


【解决方案1】:

结果集 resultSet = null; 调用更新用户后仍为空。

无论如何你都在尝试访问

servlet 将始终调用 request.setAttribute("client", user_dao.find_all());

使用 else if 除非您希望它仅在非更新情况下发生。

【讨论】:

    【解决方案2】:

    您的 HTML/JSP 错误。您创建了一个 HTML 表单,但您所说的“按钮”实际上只是一个 独立 链接。这就是您提到的两个问题的原因:

    1. 当你点击链接表单时没有提交
    2. 当您按下回车键时,表单已提交,但没有您在 URL 中输入的数据

    你需要把它改成类似

    <%
    if(action.equalsIgnoreCase("edit")){
    %>
    <form method="post" action="client">
       <div class="form-group">
            <input type="text" class="form-control" id="login" name="login" placeholder="login">
    
            <input type="hidden" id="action" value="update" >
            <input type="hidden" id="id"  value="${ user_edit.id_user }" >
            <button type="submit" class="btn btn-primary">Update</a>
       </div>
     </form>
     <% } %>
    

    侧节点:绝不使用 HTTP GET 向服务器提交任何更改。如果您更改服务器上的数据,它应该是 POST(或 DELETE,或 PUT 但不是 GET)。

    【讨论】:

      猜你喜欢
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2018-07-21
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      相关资源
      最近更新 更多