Servletでログイン画面を作成しています。
LoginServlet.java(コントローラ)でユーザ名、パスワードを受け取り、
受け取った変数をLoginServletLogin.java(ロジック)に渡し、
ロジック側でログイン可否をbooleanで判定し、
その結果をもってコントローラでログイン成功画面、またはログイン失敗画面へと移行する形にしたいのですが、
java.lang.NullPointerExceptionが出てしまいます。
デバッグモードで、変数inputId, inputPasswordにそれぞれJSPで受け取った値が入っている事は確認済みなのですが、
何が原因でNullPointerExceptionが出ているのかが掴み切れていません。
大変お手数ですが、解決の為、ヒントを頂けますと幸いです。
LoginServlet.java
package budget_tracker.login; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.naming.InitialContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; LoginServletLogic loginServletLogic; Connection conn = null; PreparedStatement pstmt = null; ResultSet rset = null; boolean sqlResult = false; // データソースの作成 DataSource ds; // 初期化処理 public void init() throws ServletException { try { // 初期コンテキストを取得 InitialContext ic = new InitialContext(); // ルックアップしてデータソースを取得 ds = (DataSource) ic.lookup("java:comp/env/jdbc/searchman"); } catch (Exception e) { } finally { try { // just in case, terminate the DB connection with finally statement conn.close(); } catch (Exception e) { } } } /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // setting text code request.setCharacterEncoding("Windows-31J"); // get the id in index.jsp String inputId = request.getParameter("id"); // get the password in index.jsp String inputPassword = request.getParameter("password"); System.out.println(inputId); System.out.println(inputPassword); try { // move on to loginResult.jsp or loginResultFailed.jsp if (loginServletLogic.login(inputId, inputPassword)) { request.getRequestDispatcher("mainSearch.jsp").forward(request, response); } else { request.getRequestDispatcher("loginFailed.jsp").forward(request, response); } // terminate the used objects rset.close(); pstmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); doGet(request, response); } finally { try { // just in case, terminate the DB connection with finally statement conn.close(); } catch (Exception e) { } } } } ``` LoginServletLogic.java ```ここに言語を入力 package budget_tracker.login; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.naming.InitialContext; import javax.sql.DataSource; public class LoginServletLogic { ResultSet rset = null; DataSource ds; Connection conn = null; PreparedStatement pstmt = null; boolean sqlResult = false; public boolean login(String inputId, String inputPassword) throws SQLException { try { // 初期コンテキストを取得 InitialContext ic = new InitialContext(); // ルックアップしてデータソースを取得 ds = (DataSource) ic.lookup("java:comp/env/jdbc/searchman"); // データソースからConnectionを取得 conn = ds.getConnection(); StringBuilder sql = new StringBuilder(); sql.append("select * from login_table where id = ? && password = ? "); pstmt = conn.prepareStatement(new String(sql)); pstmt.setString(1, inputId); pstmt.setString(2, inputPassword); rset = pstmt.executeQuery(); if (rset.next()) { sqlResult = true; } else { sqlResult = false; } } catch (Exception e) { e.printStackTrace(); } return sqlResult; } }
エラー内容
java.lang.NullPointerException
at budget_tracker.login.LoginServlet.doPost(LoginServlet.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。