jsp空指针问题,求大神

Gemini_Knight 2020-10-05 05:01:28
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>商品名称</td>
<td>商品单价</td>
<td>购买</td>
</tr>
<form action= "NewFile1.jsp" method= "post">
<tr>
<td>手机</td>
<td>2000</td>
<td><input type= "submit" name= "buy" value="购买"/>
</tr>
<input type= "hidden" name="goodname" value="手机"/>
<input type="hidden" name= "price" value= "2000"/>
</form>
<form action= "NewFile1.jsp" method= "post">
<tr>
<td>电视</td>
<td>5000</td>
<td><input type= "submit" name= "buy" value="购买"/>
</tr>
<input type= "hidden" name="goodname" value="电视"/>
<input type="hidden" name= "price" value= "5000"/>
</form>
<form action= "NewFile1.jsp" method= "post">
<tr>
<td>洗衣机</td>
<td>3000</td>
<td><input type= "submit" name= "buy" value="购买"/>
</tr>
<input type= "hidden" name="goodname" value="洗衣机"/>
<input type="hidden" name= "price" value= "3000"/>
</form>
<form action= "NewFile1.jsp" method= "post">
<tr>
<td>空调</td>
<td>4000</td>
<td><input type= "submit" name= "buy" value="购买"/>
</tr>
<input type= "hidden" name="goodname" value="空调"/>
<input type="hidden" name= "price" value= "4000"/>
</form>
<table/>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="cc.Good"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%request.setCharacterEncoding("utf-8");
String goodname=request.getParameter("goodname");
String price=request.getParameter("price");
Good good =new Good();
good.setGoodname(goodname);
good.setPrice(price);
ArrayList <Good> a=null;
HttpSession sess = request.getSession();
if(session.getAttribute("good")==null){
a=new ArrayList<Good>();
}else{
a=(ArrayList<Good>)session.getAttribute("good");
}
a.add(good);
sess.setAttribute("good", a);
response.sendRedirect("NewFile2.jsp");
%>
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="cc.Good"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>商品名称</td>
<td>商品单价</td>
<td>数量</td>
</tr>
<table/>
<%! ArrayList<Good> arry;
Good good[];
int price;%>
<%
HttpSession ses = request.getSession();
arry=(ArrayList<Good>)ses.getAttribute("good");
if(arry!=null){
for(int i=0;i<arry.size();i++){
good[i]=arry.get(i);
price+=Integer.parseInt(good[i].getPrice());
out.println(good[i].getGoodname());
out.println(good[i].getPrice());
out.println("<br/>");
}
out.println("商品总价"+price);
}
%>
<%=arry %>
</body>
</html>

求大神, good[i]=arry.get(i);为什么这里会报空指针异常啊,该怎么解决
...全文
123 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
鸡窝里的毛 2020-10-05
  • 打赏
  • 举报
回复
arry不存在,sesssion中没这个对象。

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧