• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

ArrayList populating with redundant data

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I have a repository method that is supposed to return an ArrayList.  The ResultSet I get from the database returns correctly but the ArrayList only populates with the same record 4 times.  I have 4 records in my database.

Here's my code:


I was returning a set before but I changed it to an ArrayList while I was experimenting with what was wrong.
 
Marshal
Posts: 76822
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Make your method return List<Reimbursement> rather than ArrayList<Reimbursement>
Look where you are creating the Reimbursement object (line 3). How many objects do you create as your loop runs?
 
Andrew Spiteri
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed ArrayList to LIst.  Is there a reason why that is good practice?  I see it's normally done but I don't know why.

There are 4 ReimbursementOut data transfer objects that are put into the ArrayList.  They are all of one record, though, while I have 4 different records in my database.
 
Andrew Spiteri
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll move the variable declarations outside of the loop so I'm not creating so many objects, as well.
 
Andrew Spiteri
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my reworked code.



I'm also going to include a picture of my console output from Tomcat.  It shows the JSON string I'm receiving.

JSONoutput.PNG
[Thumbnail for JSONoutput.PNG]
JSON string output to console as well as loop through ArrayList in servlet class
 
Saloon Keeper
Posts: 9715
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Move line 3 after line 21. You need to create a new object each time round the loop. Currently, you are creating a single object once and reusing the same object 4 times.
 
Campbell Ritchie
Marshal
Posts: 76822
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrew Spiteri wrote:I'll move the variable declarations outside of the loop so I'm not creating so many objects, as well.

Not a good idea, I am afraid. Declare your reference where you need it, which is inside the loop. As it is, as Carey hinted, you are only creating one object.
 
Campbell Ritchie
Marshal
Posts: 76822
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't return null. If anything goes wrong, either return the List in its present state (size=0) or let the exception propagate, or throw a different (preferably chained) exception.
I presume lines 35‑37 are only there for debugging purposes. That code will always work because the add() method always works and will always return true.
 
Campbell Ritchie
Marshal
Posts: 76822
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrew Spiteri wrote:I changed ArrayList to LIst.  Is there a reason why that is good practice?  . . . .

It means you can return any type of List, e.g. ArrayList, LinkedList, or some of the obscurer types of List you get from methods like these:- 1 2 3 or Stream.collect(Collectors.toList()).

Actually, the current implementation of Stream.collect(Collectors.toList()) produces the well‑known ArrayList.
 
get schwifty. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic