Как исправить это исключение NullPointerException с помощью request.getParameter ("password_confirmation")? - PullRequest
0 голосов
/ 13 мая 2019

Я пытался вызвать сервлет из моей формы jsp, и до сих пор это не удавалось. Он перенаправляет сервлету processRequest () и генерирует исключение java.lang.NullPointerException:

13-May-2019 18:33:30.193 SEVERE [http-nio-8084-exec-252] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [index] in context with path [/RoyalAwards] threw exception
 java.lang.NullPointerException
    at index.doPost(index.java:85)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

13-May-2019 18:44:38.618 SEVERE [http-nio-8084-exec-252] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [index] in context with path [/RoyalAwards] threw exception
 java.lang.NullPointerException
    at index.doPost(index.java:85)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

Я пытался использовать action = "$ {pageContext.request.contextPath} / index",

Я пытался убедиться, что на кнопке формы есть метод = "post",

Я пытался убедиться, что я ссылаюсь на правильные имена с помощью request.getParameter (),

Я пытался найти файл jsp-api - *. Jar для удаления (отсутствует в моем проекте),

Я попытался проверить щелчок по ошибке, и она выделила if (password.equals (password_confirmation)) {

Форма jsp:

<form action="${pageContext.request.contextPath}/index" method="post" data-remote="true" accept-charset="UTF-8">
                                            <input type="radio" name="reg_type" value="sme" checked> SME <br>
                                            <input type="radio" name="reg_type" value="freelancer"> Freelancer <br>
                                            <input id="id" class="form-control" type="text" placeholder="Identification Nmber" name="id">
                                            <input id="email" class="form-control" type="text" placeholder="Email" name="email">
                                            <input id="password" class="form-control" type="password" placeholder="Password" name="password">
                                            <input id="password_confirmation" class="form-control" type="password" placeholder="Repeat Password" name="password_confirmation">
                                            <input class="btn btn-default btn-register" type="submit" value="Create account" name="submit">
                                        </form>

сервлет:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

        String password = request.getParameter("password");
        String password_confirmation = request.getParameter("password_confirmation");
        String reg_type = request.getParameter("reg_type");
        String id = request.getParameter("id");
        String email = request.getParameter("email");
        boolean check = false;

        if(password.equals(password_confirmation)){
            try{

                UserDAO udao = new UserDAO();
                check = udao.registerUser(id, email, password);

            } catch(Exception e){
                System.out.println(password);
                System.out.println(password_confirmation);
                System.out.println(reg_type);
                System.out.println(id);
                System.out.println(email);
                System.out.println(check);
                e.printStackTrace();
            }

            if(reg_type.equals("sme")){
                if (check) {
                    //response.sendRedirect("http://localhost:8084/RoyalAwards/sme reg/SME Application Form.jsp/#email="+email);
                    RequestDispatcher view = request.getRequestDispatcher("/sme reg/SME Application Form.jsp/#email="+email);
                    view.forward(request, response);
                }else{
                    //else 
                }
            }else{
                if (check) {
                    //response.sendRedirect("http://localhost:8084/RoyalAwards/freelancer reg/Freelancer Application Form.jsp/#email="+email);
                    RequestDispatcher view = request.getRequestDispatcher("/freelancer reg/Freelancer Application Form.jsp/#email="+email);
                    view.forward(request, response);
                }else{

                }
            }

        }else{

            // error

        }

    }

метод базы данных:

public boolean registerUser(String identificationNumber, String email, String password) throws Exception {

        boolean check = false;

        try {

              String query = "insert into userinfo values('"+identificationNumber+"', '"+email+"', '"+password+"', 'pending')";

              PreparedStatement preparedStmt = con.prepareStatement(query);

              check = preparedStmt.execute();

              con.close();

              return check;

        }catch(Exception e) {
                    System.out.println(e.getLocalizedMessage());
                    throw e;
        }

    }

Строка подключения к базе данных:

public UserDAO() throws Exception {

        try{  

            Class.forName("com.mysql.jdbc.Driver");  

            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pb","root","");

            stmt = con.createStatement();

        }catch(Exception e){ 

                    System.out.println(e.getLocalizedMessage());
                    throw e;

        }  
    }

В случае успеха я ожидаю, что веб-приложение вставит данные в базу данных и перенаправит на другую страницу регистрации.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...