JSP循环索引解析失败:Cannot parse null string 问题排查
购物车删除功能NumberFormatException问题排查与解决
我已经实现了购物车基础功能,订单显示正常,但添加商品删除功能时遇到报错:制作的删除按钮本该将循环中的索引值传递到Servlet触发removeFromCart方法,其他功能正常,唯独删除功能触发以下错误:
错误消息:Cannot parse null string
错误描述:服务器遇到意外条件,无法完成请求。
异常信息:java.lang.NumberFormatException: Cannot parse null string
注意:仅注释的四行代码会触发此错误
我的JSP按钮代码
<jsp:body> <form action="addtocart" method="post"> Bottom: <select name="bottom" id="bottom"> <option value="1">Chokolade (5 kr)</option> <option value="2">Vanilje (6 kr)</option> <option value="3">Mercedes</option> <option value="4">Audi</option> </select> Topping: <select name="top" id="top"> <option value="1">Chokolade (10 kr)</option> <option value="2">Vanilje (6 kr)</option> <option value="3">Mercedes</option> <option value="4">Audi</option> </select> Quantity: <select name="quantity" id="quantity"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> <button name="addToCart"> Add to Cart</button> </form> Order lines: ${requestScope.cartsize}<br> Cart is having:<br> <c:forEach var="item" items="${sessionScope.cart.cupcakeList}" varStatus="loop"> <form action="addtocart" method="post"> Quantity ${item.quantity} Order line number: ${loop.index+1} <button name="removeFromCart" value="${loop.index}" type="submit">Remove from Cart </button></form><br> </c:forEach> </jsp:body>
我的Servlet doPost方法代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); ShoppingCart cart = (ShoppingCart) session.getAttribute("cart"); int topId = Integer.parseInt(request.getParameter("top")); int bottomId = Integer.parseInt(request.getParameter("bottom")); int quantity = Integer.parseInt(request.getParameter("quantity")); // int removeFromCart = Integer.parseInt(request.getParameter("removeFromCart")); Top top = CupcakeFacade.getTopById(topId); Bottom bottom = CupcakeFacade.getBottomById(bottomId); Cupcake cupcake = new Cupcake(top, bottom, quantity); cart.addToCart(cupcake); session.setAttribute("cart", cart); request.setAttribute("cartsize", cart.getNumberOfCupcakes()); // if(removeFromCart >= 0){ // cart.removeFromCart(removeFromCart); // } request.getRequestDispatcher("WEB-INF/welcome.jsp").forward(request, response); }
问题根源与修复方案
核心问题
当点击不同按钮时,请求携带的参数不一致:
- 点击「Add to Cart」时,请求中没有
removeFromCart参数,request.getParameter("removeFromCart")返回null,直接调用Integer.parseInt会抛出NumberFormatException - 点击删除按钮时,请求中没有
top、bottom、quantity参数,同样会触发这些参数的parseInt异常
修复后的doPost代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); ShoppingCart cart = (ShoppingCart) session.getAttribute("cart"); // 初始化购物车(防止首次访问时cart为null) if (cart == null) { cart = new ShoppingCart(); session.setAttribute("cart", cart); } // 区分添加/删除动作 if (request.getParameter("addToCart") != null) { // 处理添加商品:先校验参数存在 String topParam = request.getParameter("top"); String bottomParam = request.getParameter("bottom"); String quantityParam = request.getParameter("quantity"); if (topParam != null && bottomParam != null && quantityParam != null) { int topId = Integer.parseInt(topParam); int bottomId = Integer.parseInt(bottomParam); int quantity = Integer.parseInt(quantityParam); Top top = CupcakeFacade.getTopById(topId); Bottom bottom = CupcakeFacade.getBottomById(bottomId); Cupcake cupcake = new Cupcake(top, bottom, quantity); cart.addToCart(cupcake); } } else if (request.getParameter("removeFromCart") != null) { // 处理删除商品:校验参数存在且索引有效 String removeParam = request.getParameter("removeFromCart"); if (removeParam != null) { int removeIndex = Integer.parseInt(removeParam); // 防止索引越界 if (removeIndex >= 0 && removeIndex < cart.getCupcakeList().size()) { cart.removeFromCart(removeIndex); } } } // 更新购物车状态并跳转 session.setAttribute("cart", cart); request.setAttribute("cartsize", cart.getNumberOfCupcakes()); request.getRequestDispatcher("WEB-INF/welcome.jsp").forward(request, response); }
额外优化建议
- JSP中可给按钮添加明确的动作标记(比如隐藏字段),进一步避免动作判断歧义
- 给前端表单添加必填校验,提前拦截空值提交
- 始终校验索引范围,防止删除时触发IndexOutOfBoundsException
内容的提问来源于stack exchange,提问作者Narj
相关产品推荐
相关产品推荐

