2017-03-16 10 views
0

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> 
<% } %> 

そして、私のサーブレット:私はボタンでクリックしたときに

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

私の最初の問題は、ある「更新 ここに私のDAOです"、それは動作します。しかし、もし私がEnterキーを押すとnullポインタ例外が発生する

2番目の問題は、String login = request.getParameter("login");user.setLogin(login);でログインユーザーを回復したい場合、ログイン値がdbでNULLになることです。

どうもありがとう

EDIT: ここにスタックされたトレース:

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は、空
+0

スタックトレースとは何ですか?あなたはそれをデバッグするために何をしましたか? – Carcigenicate

+0

がスタックを追加しました。ありがとう – Snake

答えて

0

のResultSet結果セット= NULLです。 更新ユーザーが呼び出された後もnullです。あなたは、サーブレットは常にrequest.setAttribute(「クライアント」、user_dao.find_all())を呼び出します

アクセスしようとしているいずれの場合においても

非更新の場合にのみ発生させたい場合を除き、else ifを使用します。

0

あなたのHTML/JSPが間違っています。あなたはHTMLフォームを作成しますが、 "ボタン"は実際にはとなります。リンクです。これは、あなたが言及両方の問題の理由です:

  1. あなたは、リンクフォームが
  2. Enterキーを押す
  3. は、フォームが送信されますが、データなしで、あなたが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> 
<% } %> 

側ノードのようなものに変更する必要があります。NEVER HTTP GETを使用して、サーバーに変更を送信します。サーバー上のデータを変更する場合は、POST(またはDELETE、またはPUTではなくGET)にする必要があります。

関連する問題