• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble with ArrayList  RSS feed

 
Patrick Mala
Greenhorn
Posts: 9
Android Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm having some trouble with an arraylist of objects. When I try retrieving elements from the list, they are all duplicates of the last element.
Here's the problematic code segment:



The word "Keyboard" is printed out twelve times, instead of all the other item names.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us your StoreItem class. The only way I can see for that to happen is if the variable that holds the name in that class is static.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

Please don't write such long lines; they are difficult to read. I might be able to sort your post out for you, and show you how it should be done.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Retina technology? That would be really useful, to stop people sitting in my chair
 
Patrick Mala
Greenhorn
Posts: 9
Android Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies, I don't think the actual StoreItem class is causing the issue but here it is anyway:


Here's my main Store class as well if that helps.

 
Patrick Mala
Greenhorn
Posts: 9
Android Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Can you show us your StoreItem class. The only way I can see for that to happen is if the variable that holds the name in that class is static.


Doh!
So that's why that wasn't working. I had made all the fields static because I was getting static reference errors in my sorting code, so I just changed those back to static.
When I looked back, I realized I had made an error (StoreItem.method() instead of item.method() ) in some another part. When I removed that static part and fixed the typo, it worked fine!
Thanks for the help
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Making things static to avoid such compiler errors is usually a mistake. The correct way to avoid that compiler error is to move the instance field's reference out of a static context.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!