Getting cannot convert object to ArrayList error when retrieving ArrayList from HttpSession
-
20-09-2019 - |
Question
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.
Solution
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");
OTHER TIPS
You have to cast it.
sriList = (ArrayList)session.getAttribute("scannedMatches");
try this:
Object scannedMatchesObj = session.getAttribute("scannedMatches");
if ( scannedmatchesObj instanceOf List ){
sriList = (ArrayList)scannedMatchesObj;
//Do your stuff...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow