wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Error with Arraylist object returning from SessionBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Error with Arraylist object returning from SessionBean" Watch "Error with Arraylist object returning from SessionBean" New topic
Author

Error with Arraylist object returning from SessionBean

Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
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:



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




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:


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());
}



It prints the same thing again and again though value of i is incremented. Why is this eror coming?
Please help.
IJ
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

You are overwriting the same object each and everytime you add an item.
You should initialize the itemDetails object each time in the loop, else it will point to the previously added itemDetails object and overwrite that data.
Your code should be


ram.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

"Indiana Jones",
As asked by Ajith, please update your display name to comply with our JavaRanch Naming Policy.

In particular, your display name must not be obviously fictitious.

Thanks,
Jeanne
Forum Bartender


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
Hello Ram,
I did what you had told, but it doesn't help. I am still getting the same data for all the list contents.
Also, This is the output of the log file, which is showing the output of System.out.prinltln that I have put in the same while loop where I am storing the data in the object fields. As you can see I get name of the item correctly when I display it from the list.



I even tried what you said but it doesn't help. What can be the reason?
Thanks,
Abhishek (IJ)
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

I cant find anything else that's wrong with your code except that I suggested.

When you have an Object pointed to with a reference as below


and add that object to a list using that refernce,


anything operation on the Object henceforth would result in the Object inside the List getting altered. For eg



And that's what's erroneous in your code (as far as I can see). To add a different object using the same reference, you would have to change your reference to point to the new Object



ram.
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi Abhishek,

Ram is definitely right and if you made his changes your code should work. I actually intended to post a similar answer to your posting this passed weekend, but Ram was just seconds ahead me. Because it made no sense to have two identical answers I ultimately canceled my reply. However my point here is that if two different people make the same observation independently, then it must be true.
There is one more hint I could suggest to you. Check your build again, make sure you�ve deployed the new version and from my experience I know that sometimes the application server could be the source of your errors. I remember WebLogic 6.1 gave me sometimes a hard time, because the new versions were not re-deployed correctly. The server maintained an internal cache and therefore I had to physically remove that folder from the disk and restart the server. You might like following this path as well.
Regards.


I think, therefore I exist -- Rene Descartes
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

"Abhishek",
Thanks for changing the display name. However, we require two names: a first name and a last name. Please add a second name.

Thanks,
Jeanne
Forum Bartender
Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
Thanks a lot Ram and Valentin,
Your observations wer exactly right and as you said Valentin, I had to redeploy the bean.
Thanks a lot for the help guys.
Abhishek
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error with Arraylist object returning from SessionBean
 
Similar Threads
PreparedStatement (displaying records)
why is the List returning empty?
Displaying records through result set meta data
Problem with c:foreach not working
Dynamic table not displaying.