Storing a reference to the object in ArrayList

Hey everyone i am trying to store the current reference to an arraylist "pl". e.g. pl.add(this); for some reason i only get the reference to the last item and none of the previous ones. The Loop does go through all three items tho.

Below is the code and out put i am getting. Can anyone tell me what i am doing wrong thank you for the help in advance.

       // variables
private String productType;
private String hyperLinkParam;
private ArrayList <ProductList> pl = new ArrayList<ProductList> ();

public ProductList() {

    try {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

       InputStream url = null;
        url = getClass().getResourceAsStream("inventory.xml");


        Document doc = db.parse(url);
        doc.getDocumentElement().normalize();

        // loop through each item
        NodeList items = doc.getElementsByTagName("item"); //Returns a list of elements with the given tag name item
        for (int i = 0; i < items.getLength(); i++)
        {

                Element e = (Element) items.item(i);
                setHyperLinkParam(e.getAttribute("name").toString());
                setProductType(getTextValue(e,"productType"));
                System.out.print(e.getAttribute("name").toString());
                System.out.println(getTextValue(e,"productType"));

                pl.add(this);


         }

          for(int j=0; j < pl.size(); j++){
            System.out.print("getHyperLinkParam: " + pl.get(j).getHyperLinkParam());
            System.out.println("getProductType: " + pl.get(j).getProductType());
          }

Manufacture.java

    @WebMethod(operationName = "getProductList")
public ProductList getProductList() {
    try {
        ProductList productlist = new ProductList();
        if(productlist == null){
            return null;
        }else{
            return productlist;
        }
    } catch(Exception e){
        System.out.println("error: " + e.getMessage());
        return null;
    }
}

index.jsp

    <%
try {
org.soen487.supplychain.manufacturer.Manufacture_Service service = new org.soen487.supplychain.manufacturer.Manufacture_Service();
org.soen487.supplychain.manufacturer.Manufacture port = service.getManufacturePort();
// TODO process result here
org.soen487.supplychain.manufacturer.ProductList result = port.getProductList();
out.println("Result = "+result);

} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
Jon Skeet
people
quotationmark

This is the problem:

pl.add(this);

You're adding the same reference (this) again and again. The value of this is just a reference to the "current" object - and you're modifying the contents of that object in the loop.

In each iteration of the loop you should create a new, separate object, set the properties on that object, and then add a reference to that object to the list.

It's slightly odd that you'd be adding this to the list at all, to be honest - usually a method like this would be in some class which knew how to parse the XML, not an instance of the data item itself. It's not clear where pl is declared, but you should really consider the structure of your program.

people

See more on this question at Stackoverflow