Search code examples
javaarraylistxstream

Can't reach ArrayList content


I've been parsing XML into ArrayList using Xsrteam. It worked but for some reason I can't reach array's content.

Classes which handle ArrayList data:

@XStreamAlias("BOARDS")
public class Boards {
    @XStreamImplicit(itemFieldName = "id")
    public ArrayList ids = new ArrayList();

    //region GETTERS-SETTERS
    public void setIds(ArrayList temp){
        this.ids = temp;
    }
    public List getIds(){
        return ids;
    }
//    endregion
}

@XStreamAlias("id")
class Id {

    @XStreamAlias("board")
    private String board;

    @XStreamAlias("description")
    private String description;

    @XStreamAlias("price")
    private String price;

    @XStreamAlias("shape")
    private String shape;

    @XStreamAlias("riding_level")
    private String ridingLevel;

    @XStreamAlias("riding_style")
    private String ridingStyle;

    @XStreamAlias("camber_profile")
    private String camber;

    @XStreamAlias("stance")
    private String stance;

    @XStreamAlias("picture")
    private String picture;

    <<public getters - setters here>>
}

How I tried to access those getters:

Boards boards = (Boards) xstream.fromXML(reader); // parse xml into array list
boards.getIds().get(0).getPrice(); //!!getPrice() cannot be resolved

first is Object first = boards.getIds().get(0);

Here what it looks like using debugger:

enter image description here


Solution

  • Boards has a raw type, because of that it is unclear, what object types should be inside ArrayList ids. So, you should either cast result of boards.getIds().get(0) explicitly:

    ((Id) boards.getIds().get(0)).getPrice()
    

    or generify Boards class:

    public class Boards<E> {
    
        public List<E> ids = new ArrayList<E>();
    
        //region GETTERS-SETTERS
        public void setIds(ArrayList<E> temp){
            this.ids = temp;
        }
        public List<E> getIds(){
            return ids;
        }
    //    endregion
    }
    

    You can read about generics here