0

I am trying to retrieve information from an ArrayList that I set in the session variables. But it isn't being set correctly some where because I get a null pointer when I run searchList.isEmpty()
The servlet part:

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. The 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!

2
  • 1
    seems you are not calling correct attibute. You are filling 'searchList' but after, you are calling 'searchProduct' Commented May 8, 2016 at 17:16
  • You set as session.setAttribute("searchList", searchList); therefore you should use ArrayList<Product> searchList = (ArrayList<Product>)session.getAttribute("searchList"); Commented May 8, 2016 at 17:20

1 Answer 1

1

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"); searchProduct isn't set/doesn't exist and so,

session.getAttribute("searchProduct");

returns null and gives a nullpointerexception upon calling isEmpty().

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my goodness. I've been looking at this thing forever. Thank you Cedric absolutely fixed the problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.