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は、空
スタックトレースとは何ですか?あなたはそれをデバッグするために何をしましたか? – Carcigenicate
がスタックを追加しました。ありがとう – Snake