gpt4 book ai didi

java - JSP抛出空指针异常

转载 作者:行者123 更新时间:2023-12-02 10:07:11 25 4
gpt4 key购买 nike

我不断收到空指针异常,并且无法从堆栈跟踪中准确判断出导致该异常的原因。它似乎指向 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)

最佳答案

这里

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

帐户 为空。您尚未使用

设置名称为 customer.listHttpSession 参数
HttpSession#setAttribute(String name, Object o)

( setAttribute ) 在您的 LoginServlet#processRequest 方法中。那么,当

if (pw.equals(pwDB)){

您将转发到 DisplayAccount.jsp 但没有该值。

<小时/>

您需要添加

session.setAttribute("customer.list", customerList);
<小时/>

你不能做你想做的事,你需要另一个属性

session.setAttribute("customerList", c1.list); // or c1.getList() if you have a getter

关于java - JSP抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55267818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com