Servlet代码抛出NullPointer异常

问题描述 投票:0回答:2

我已经编写了一个简单的登录代码,可以通过使用Java servlet登录到帐户来访问mysql。

代码运行正常,所有页面都来自注册页面->登录页面->添加详细信息页面->注销页面。这正确发生。但是每次我登录时,控制台中都会出现一个空指针异常。

我检查了所有其他内容,并确保在我的catch块中添加了其他内容,以确保它不是sql或可能引发错误的任何其他组件或变量。

如果是HttpSession,谁能帮助我弄清楚如何处理它?还是不用担心?

================================================ ======================================>

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

        String uname = request.getParameter("uname");

        String pwd = request.getParameter("pwd");

        HttpSession s = request.getSession();
        s.setAttribute("uname", uname);
        s.setAttribute("password", pwd);


        if(uname.length()==0) {
            errors=true;
            request.setAttribute("error_name", true);
        }
        if(pwd.length()==0) {
            errors=true;
            request.setAttribute("error_pwd", true);
        }
        //--------------------------------------------------------------------------------------------



        //--------------------------------------------------------------------------------------------
        if(errors == true) {

            views = request.getRequestDispatcher("WEB-INF/views/Login.jsp");
            views.forward(request, response);
        }else {

            try {

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

                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Registry","root","");
                Statement st = conn.createStatement();

                rs = st.executeQuery("select * from Users;");

                if(rs!=null) {
                    while (rs.next())
                    {
                        if((rs.getString("username").contentEquals(uname))==false ) {
                            request.setAttribute("login_error",true);
                        }else {
                            //request.setAttribute("login_error",null);
                            //System.out.println("username match");
                            System.out.println("username match");


                            if((rs.getString("password").contentEquals(pwd))==false ) {

                                request.setAttribute("login_error",true);

                            }else {

                                //System.out.println("password match");
                                System.out.println("password match");
                                //System.out.println("entry found!");
                                System.out.println("Redirect to UserPage!");
                                views = request.getRequestDispatcher("WEB-INF/views/User_page.jsp");
                                views.forward(request, response);
                                break;
                                }
                            }
                        }
                    if(request.getAttribute("login_error").equals(true)) {
                        views = request.getRequestDispatcher("WEB-INF/views/Login.jsp");
                        views.forward(request, response);
                    }else {
                        System.out.println("login_error = null");
                    }

                }else {
                    request.setAttribute("empty_res", true);
                    System.out.println("Empty result!");
                    System.out.println("rs = "+rs);
                }


                conn.close();
            }catch (SQLException ex) {
                    System.out.println("====================================================");
                    System.out.println("login exception!");
                    System.out.println(ex);

            }catch (ClassNotFoundException e) {
                System.out.println("====================================================");
                System.out.println("e : "+e);
                }
            }
        }
    }

================================================ ============================================输出:

username match
password match
Redirect to UserPage!
Feb 06, 2020 6:08:20 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [login] in context with path [/Loginapp1] threw exception
java.lang.NullPointerException
    at login.doPost(login.java:102)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:165)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1195)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:317)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:834)

我已经编写了一个简单的登录代码,可以通过使用Java servlet登录到帐户来访问mysql。代码运行良好,所有页面都从注册页面->登录页面->添加详细信息页面-> ...

java exception servlets nullpointerexception httpsession
2个回答
0
投票

我认为此行的if(request.getAttribute("login_error").equals(true))处发生了异常。一旦用户匹配并且密码匹配。这意味着此行request.setAttribute("login_error",true);从未执行。当用户或密码不匹配时,您已经写了两次。但是由于它们是匹配的,因此请求中没有名为login_error的属性。因此,在if语句中访问它时。它将为空。这没什么好担心的。如果要删除它,请放在此行


0
投票

如果有:

© www.soinside.com 2019 - 2024. All rights reserved.