Getting cannot convert object to ArrayList error when retrieving ArrayList from HttpSession
I have saved an ArrayList to the session object. I am trying to retrieve it using
sriList 开发者_JAVA百科= 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.
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");
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...
}
精彩评论