views:

368

answers:

3

I have saved an ArrayList to the session object. I am trying to retrieve it using

sriList = session.getAttribute("scannedMatches");

I am getting the compile time error "Cannot convert from Object to ArrayList". How can I get my ArrayList back from the session object.

+4  A: 

The HttpSession#getAttribute() method returns java.lang.Object:

public java.lang.Object getAttribute(java.lang.String name)

Did you try to cast the returned object?

sriList = (ArrayList)session.getAttribute("scannedMatches");
Pascal Thivent
+1 `The HttpSession#getAttribute() method returns java.lang.Object:`
Rakesh Juyal
Thanks - normally my IDE recommends a cast when it makes sense, so I thought there must be someting different here.
Ankur
Is this a sign we are becoming to IDE dependant? I remember programming my first apps in notepad with command line javac... my first BIG program I did without autocomplete...
Zoidberg
in notepad!! what are you saying Zoid.
Rakesh Juyal
+3  A: 

You have to cast it.

sriList = (ArrayList)session.getAttribute("scannedMatches");
Vincent Ramdhanie
+1  A: 

try this:

Object scannedMatchesObj = session.getAttribute("scannedMatches");
if ( scannedmatchesObj instanceOf List ){
    sriList = (ArrayList)scannedMatchesObj;
    //Do your stuff...
}
Rakesh Juyal
Just becuase (scannedmatchesObj instanceof List) is true, doesn't necessarily mean it's an ArrayList. Might be a better idea here, depending on the circumstances, to declare sriList as a List, and cast accordingly.
joev