JSP抛出空指针异常

时间:2019-03-20 18:28:43

标签: java jsp

我不断收到Null指针异常,并且我无法从堆栈跟踪中确切知道是什么原因引起的。似乎指向DisplayAccounts.jsp中的第83行,但是文件只有48行... 登录后,应该使用LoginServlet将用户定向到DisplayAccounts.jsp

任何帮助将不胜感激。我似乎无法弄清楚。

DisplayAccounts.jsp:

    <h1>Customer Accounts</h1>                
    <table>
        <tr>
            <th>Account Number</th>
            <th>Customer ID</th>
            <th>Account Type</th>
            <th>Account Balance</th>
        </tr>
    <% ArrayList accounts = (ArrayList)session.getAttribute("customer.list");
    for(int i = 0; i < accounts.size(); i++) {
        Account account = (Account)accounts.get(i); %>
        <tr>
            <td><% out.print(account.getAcctNo());%></td>
            <td><% out.print(account.getcID());%></td>
            <td><% out.print(account.getType());%></td>
            <td><% out.print(account.getBalance());%></td>
        </tr>
    <% } %>
    </table>

LoginServlet.java:

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String id;
        String pw;
        int idDB;
        String pwDB;
        HttpSession session;
        try{
            id = request.getParameter("custID");
            pw = request.getParameter("pswd");
            System.out.println("ID: " + id);
            System.out.println("Password: " + pw);
            idDB = Integer.parseInt(id);

            Customer c1 = new Customer();
            c1.selectDB(idDB);
            pwDB = c1.getCustPassword();                
            // Add user to session
            session = request.getSession();
            session.setAttribute("customer", c1);
            System.out.println("User added to session");
            if (pw.equals(pwDB)){
                // Send user to account look up page
                RequestDispatcher rd = request.getRequestDispatcher("DisplayAccount.jsp");
                rd.forward(request, response);
            }

            else{
                // Send user to error page
                RequestDispatcher rd = request.getRequestDispatcher("ErrorPage.jsp");
                rd.forward(request, response);
            }
        }
        catch(ServletException e){
            System.out.println("A Servlet Excpetion has occured");
            System.out.println(e.getMessage());
            System.out.println(e.getStackTrace());
        }
        catch(IOException e){
            System.out.println("An IO Exception has occured");
            System.out.println(e.getMessage());
        }
        catch(Exception e){
            System.out.println("An error has occurred.");
            System.out.println(e.getMessage());
        }
    }

异常堆栈跟踪:

Info:   A Servlet Excpetion has occured
Info:   java.lang.NullPointerException
Info:   java.lang.NullPointerException
    at org.apache.jsp.DisplayAccount_jsp._jspService(DisplayAccount_jsp.java:83)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:575)
    at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:546)
    at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)
    at Chatt_Bank.LoginServlet.processRequest(LoginServlet.java:45)
    at Chatt_Bank.LoginServlet.doPost(LoginServlet.java:80)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:1)

这里

ArrayList accounts = (ArrayList)session.getAttribute("customer.list");

accounts为空。您尚未使用

设置名称为HttpSession的{​​{1}}参数
customer.list
您的HttpSession#setAttribute(String name, Object o) 方法中的

setAttribute)。所以,什么时候

LoginServlet#processRequest

您要转发到if (pw.equals(pwDB)){ ,而没有该值


您需要添加

DisplayAccount.jsp

您无法做自己想做的事,需要另一个属性

session.setAttribute("customer.list", customerList);