Error with Arraylist object returning from SessionBean

Discussions

EJB programming & troubleshooting: Error with Arraylist object returning from SessionBean

  1. Hi,
    I am accessing a database from a stateless session bean through a DAO. I am accessing all the fields and storing the values in an object of a class I have made. The class has datamembers correspondng to the fields of the table and getter and setter methods.
    I am storing all the records as objects in an ArrayList object. This is the code of DAO Implementation class:

    [code]
    try{
    con=jdbcFactory.getConnection();
    String query="select * from itemgeneralDetails";
    ps=con.createStatement();
    rs=ps.executeQuery(query);
    while(rs.next())
    {

    System.out.println(rs.getString("itemname"));

    itemDetails.setItemID(rs.getString("itemid"));
    itemDetails.setItemName(rs.getString("itemname"));
    itemDetails.setCategory(rs.getString("category"));
    itemDetails.setSubcategory1(rs.getString("subcategory1"));
    itemDetails.setSubcategory2(rs.getString("subcategory2"));
    itemDetails.setSellingprice(Double.parseDouble(rs.getString("sellingprice")));
    itemDetails.setSellingUnit(rs.getString("sellingunit"));
    itemDetails.setDiscount(Double.parseDouble(rs.getString("discount")));
    itemDetails.setWeight(rs.getString("weight"));
    itemDetails.setImageURL(rs.getString("imageURL"));
    System.out.println(itemDetails.getItemName());
    list.add(i,itemDetails);
    String testName=((ItemDetails)list.get(i)).getItemName();
    System.out.println("Name from list " + testName);
    i++;
    }
    return(list);
    [/code]

    This is giving correct result when I see the output in the log file. That is all the records are added to the list.
    But when I access this list object from my client I find that all the elements of the list have the values of last record. This is the client code:
    [code]
    ArrayList result=new ArrayList();
    result=myBean.giveResults();
    System.out.println(result.isEmpty());
    int k=result.size();

    //System.out.println("Showing RecordNo. " + i);
    itemDetails=(ItemDetails)(result.get(1));


    for(i=0;i<k;i++)
    {
    System.out.println("Showing RecordNo. " + i);
    itemDetails=(ItemDetails)result.get(i);

    System.out.println("Name : "+ itemDetails.getItemName());
    System.out.println("ID : "+ itemDetails.getItemID());
    System.out.println("Category : "+ itemDetails.getCategory());
    } [/code]

    It prints the same thing again and again though value of i is incremented. Why is this eror coming?
    Please help.
    Asty.
  2. You reuse the same object[ Go to top ]

    There is only one value ever assigned to itemDetails, so you end up adding a reference to the same object over and over again, changing the properties each time so that all the ArrayList elements point at one object holding the data from the last row.

    To fix this, add "itemDetails = new ItemDetails();" at the start of your loop.