Search code examples
javaservletscastinghttpsession

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 = 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");