开发者

Getting cannot convert object to ArrayList error when retrieving ArrayList from HttpSession

开发者 https://www.devze.com 2022-12-11 15:07 出处:网络
I have saved an ArrayList to the session object. I am trying to retrieve it using sriList 开发者_JAVA百科= session.getAttribute(\"scannedMatches\");

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...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消