user3586514
user3586514

Reputation: 123

Java ArrayList acting up

I am running in to an odd issue. I have a database with 6 different "bids". I am trying to extract the bids with the following code:

public ArrayList<ProjectBid> getProjectBids(String projectID)
    {
        ArrayList<ProjectBid> result = new ArrayList<ProjectBid>();

        ProjectBid bid = new ProjectBid();

        try{

        st = con.createStatement();
        String query = "SELECT * FROM bids WHERE projectID=\""+projectID+"\"";
        System.out.println(query);
        ResultSet rs = st.executeQuery(query);


        while (rs.next())
          {
            bid.setProjectID(projectID);
            bid.setBidID(rs.getString("bidID"));
            bid.setBidderEmail(rs.getString("bidder_email"));
            bid.setBidAmount(rs.getInt("bid_amount"));
            bid.setBidMessage(rs.getString("bid_message"));
            bid.setTimestamp(rs.getString("timestamp"));

            result.add(bid);
          }
          st.close();

        }
        catch(Exception ex){
            System.out.println("Exception: "+ex);
        }

        return result;          
    }

For some reason my ArrayList result is giving me 6 identical "bid" objects instead of adding each of the "bids" from my database individually. The code runs through the ResultSet as expected and the correct values for each run is printed correctly if I add "System.out.println(bid.getBidID);" inside my while-loop. Can anyone tell me what is wrong with my code?

Thank you

Upvotes: 0

Views: 53

Answers (3)

Samarth
Samarth

Reputation: 61

Since the reference to the bid is always the same thereby, it is overwriting the bid with the current value that in turn is changing all the bids in the result list giving you the last bid added.

Instantiate the bid object inside your loop and this should work fine.

while (rs.next())
  {
    ProjectBid bid = new ProjectBid();
    ...
    ...
    ...
    result.add(bid);
  }

Upvotes: 1

S. Pauk
S. Pauk

Reputation: 5318

You should move ProjectBid bid = new ProjectBid(); inside the while loop. With the current code you are updating the same bid variable which is defined outside the while loop hence it gets the last record's value after the loop completes.

Upvotes: 3

Aniket Thakur
Aniket Thakur

Reputation: 68975

Add

ProjectBid bid = new ProjectBid();

in your while loop. As of now you are just overwriting the same ProjectBid object. So each reference in you ArrayList is pointing to the same object value of which you keep changing in your loop. So at the end you will have all elements in ArrayList with data same as last row retrieved from the database.

    while (rs.next())
      {
        ProjectBid bid = new ProjectBid();
        bid.setProjectID(projectID);
        bid.setBidID(rs.getString("bidID"));
        bid.setBidderEmail(rs.getString("bidder_email"));
        bid.setBidAmount(rs.getInt("bid_amount"));
        bid.setBidMessage(rs.getString("bid_message"));
        bid.setTimestamp(rs.getString("timestamp"));

        result.add(bid);
      }

Upvotes: 3

Related Questions