之乌猪 2021-12-06 18:14 采纳率: 40%
浏览 1167
已结题

Servlet.service()引发了java.lang.NullPointerException java.lang.NullPointerException

严重: 在路径为/Exp3的上下文中,Servlet[Exp3.Servlet117]的Servlet.service()引发了具有根本原因的异常java.lang.NullPointerException
java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseFloat(Unknown Source)
at java.lang.Float.parseFloat(Unknown Source)
at org.apache.jsp._191220117_002dcart_jsp._jspService(_191220117_002dcart_jsp.java:155)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
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:52)
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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:713)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:462)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:387)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:315)
at Exp3.Servlet117.doPost(Servlet117.java:66)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
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:52)
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:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:544)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:616)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:831)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1634)
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)
这是出现的问题
servlet117的代码如下:
package Exp3;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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.servlet.http.HttpSession;

/**

  • Servlet implementation class Servlet117

  • /
    @WebServlet("/Servlet117")
    public class Servlet117 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**

    • @see HttpServlet#HttpServlet()
    • /
      public Servlet117() {
      super();
      // TODO Auto-generated constructor stub
      }
      public void destroy() {
      super.destroy();
      }
      /**
    • @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
      doGet(request, response);
      HttpSession session=request.getSession(true);
      String[][] goods=(String[][])session.getAttribute("MYCART");
      if(goods==null) {
      }
      float total=0;
      for(int i=0;i<goods.length;i++) {
        float amount=Float.parseFloat(goods[i][2])*Integer.parseInt(goods[i][3]);
        if(amount==0)
            continue;
        total += amount;
      
      }
        double discount=1;
        if(total >=1000)
            discount=0.85;
        else if(total >=500)
            discount=0.9;
        else if(total >=200)
            discount=0.95;
        else if(total >=100)
            discount=0.98;
        String url="191220117-cart.jsp?DISCOUNT="+discount+"&TOTAL"+total;
        RequestDispatcher rd=request.getRequestDispatcher(url);
        rd.forward(request, response);
      
      }
      public void init() throws ServletException{
      }
      }
      以下是cart.jsp的代码:
      <%
      String cardNo = (String)session.getAttribute("CARDNO");

    String[][] goods = (String[][])session.getAttribute("MYCART");

      if (goods != null){
      }    
      
      String strTotal=request.getParameter("TOTAL");
      String strDiscount=request.getParameter("DISCOUNT");
      float total=Float.parseFloat(strTotal);
      double discount=Double.parseDouble(strDiscount);
    

    %>

    查看购物车

    会员卡号:<%=cardNo %> <%for (int i=0;i <%} %>
              <tr>
                  <td colspan = "4" style = "text-align:right;">总金额:</td>
                  <td colspan = "1" style = "text-align:right;"><b><%=total %></b></td>
              </tr>
              <tr>
              <td></td>
                  <td colspan = "2" style = "text-align:right;">适用折扣:</td>
                  <td colspan = "1" style = "text-align:left;"><b><%=discount*100 %>%</b></td>
                  <td colspan = "1" style = "text-align:right;">折后金额:</td>
                  <td colspan = "1" style = "text-align:right;"><b><%=String.format("%.2f",total*discount) %></b></td>
                  </tr>
                  <tr>
                  <td></td>
                  <td colspan = "4" style = "text-align:center;">
                  <input type = "submit" value = "&nbsp;确认下单&nbsp;" name = "submit"/>&nbsp;&nbsp;
                  <input type = "button" value = "&nbsp;&nbsp;返回&nbsp;&nbsp" name = "goback" onclick = "location='191220117-shop.jsp'"/>&nbsp;&nbsp;
                  </td>
              </tr>
          </table>
      </form>
    
    编号商品名称单价(元)进入购物车选购数量
    <%=goods[i][0] %><%=goods[i][1] %><%=goods[i][2] %><%=goods[i][3] %><%=amount %>
  • 写回答

1条回答 默认 最新

  • BCS-点心 2021-12-07 09:54
    关注

    at Exp3.Servlet117.doPost(Servlet117.java:66)
    这个地方空指针异常,打断点看看

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月16日
  • 已采纳回答 12月8日
  • 修改了问题 12月6日
  • 创建了问题 12月6日

悬赏问题

  • ¥15 stc -isp烧录时显示通信超时
  • ¥15 VS2022的代码折叠加减号不见了怎么办?
  • ¥15 电脑登录粤政易软件,二维码一直空白,或账号密码登录提示服务器登录失败,是什么原因(相关搜索:防火墙)
  • ¥30 微博热门内容爬虫报错
  • ¥15 selenium获取非固定位置的元素
  • ¥50 手写签名不能上传的问题
  • ¥30 linux odbc怎么添加gbase数据库
  • ¥20 电脑开机黑屏,只有一个鼠标,联想zj者y7000
  • ¥20 DXSDK_jun10
  • ¥20 请问这种量表怎么用spss量化分析(作为中介模型的因变量