[英]Retrieving a ArrayList from Session variables
I am trying to retrieve information from an ArrayList that I set in the session variables. 我正在尝试从我在会话变量中设置的ArrayList检索信息。 But it isn't being set correctly some where because I get a null pointer when I run searchList.isEmpty() 但这在某些地方没有正确设置,因为在运行searchList.isEmpty()时得到空指针
The servlet part: Servlet部分:
case "searchProducts":
ArrayList<Product> searchList = new ArrayList<>();//create array
Product testProduct = new Product(1500,"test","testing",100); //create product
searchList.add(testProduct); //add product to ArrayList
session.setAttribute("searchList", searchList);//sets session value to ArrayList
view = request.getRequestDispatcher("SearchProduct.jsp"); //set view to JSP
break;
The JSP where I'm trying to get the info looks like this, I'm including the different things I've tried. 我试图获取信息的JSP看起来像这样,我包括了我尝试过的各种东西。 The JSP: JSP:
<%
ProductService ps = new ProductService();
ArrayList<Product> searchList = (ArrayList<Product>)session.getAttribute("searchProduct");
out.println(searchList.isEmpty());
//end test items
// if(searchList.isEmpty()== false){
// for(int count = 0; count < searchList.size(); count++){
// out.println("<option>");
// out.println(searchList.get(count).getName());
// out.println("</option>");
// }//end for
// }//end if
%>
Any help is greatly appreciated! 任何帮助是极大的赞赏!
Typo in your code. 在您的代码中输入错误。 You used "searchList" as key when you set the attribute but when you try to retreive it , you use session.getAttribute("searchProduct"); 设置属性时,将“ searchList”用作键,但是尝试检索该属性时,则使用session.getAttribute(“ searchProduct”); searchProduct isn't set/doesn't exist and so, searchProduct未设置/不存在,因此,
session.getAttribute("searchProduct"); session.getAttribute(“ searchProduct”);
returns null and gives a nullpointerexception upon calling isEmpty(). 返回null并在调用isEmpty()时给出nullpointerexception。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.