2016-05-08 5 views
0

セッション変数で設定したArrayListから情報を取得しようとしています。私はsearchList.isEmptyを実行したとき、私はNULLポインタを取得するので、しかし、それは正確にいくつかの場所に設定されていない()
サーブレット一部:私は情報がどのように見える取得しようとしているセッション変数からArrayListを取得する

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; 

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 
%> 

は、すべてのヘルプは大歓迎です!

+1

正しいアトリビュートを呼び出していないようです。あなたは 'searchList'を埋めていますが、後で 'searchProduct'を呼び出しています – OscarBcn

+0

あなたはsession.setAttribute( "searchList"、searchList)として設定します。したがって、ArrayListを使用する必要があります searchList =(ArrayList )session.getAttribute( "searchList"); – rickz

答えて

1

あなたのコードに入力ミス。 属性を設定するときにキーとして「searchList」を使用しましたが、取得しようとするとsession.getAttribute( "searchProduct");を使用します。 検索商品が設定されていませんので、

session.getAttribute( "searchProduct");

はnullを返し、isEmpty()を呼び出すとnullpointerexceptionを返します。

+0

ああ私の良さ。私はこのことを永遠に見てきました。ありがとうございましたセドリックは絶対に問題を解決しました:) – Kevin

+0

np、正しい答えを受け入れることを忘れないでください:) – Programmer1994

関連する問題