UserBlanko
UserBlanko

Reputation: 202

ArrayList adding not working outside of loop

I know StackOver flow is not a place where you can post your homework up and ask people to do it for you but i am at my wit's end I tried simple debugging method like printing line where I can see the result, just some how the arraylist doesn't save the item when i add in

lets just say the array adds two item

  first item(name1,job1,event1,date1,location1)
  second item(name2,job2,event2,date2,location2)

The end results

name1 test234

name2 test234

name2 outloop

name2 outloop

Here is the code

public class Homework {
// ...
    private int cin;
    private String[] jobarray;
    private String[] eventarray;
    private Timetable tb;
    private String[] namesarray;
//...

    public ArrayList retrievebyname(String name, String date) {
        ResultSet rs = null;
        DBController db = new DBController();
        ArrayList<Timetable> list = new ArrayList();

        // start of for loop**
        for (int k = 0; k < cin; k++) {
            // step 1 - establish connection to database
            db.getConnection();

            // step 2 - declare the SQL statement
            String dbQuery = "select event.eventName,Timetable.location,Timetable.date "
                    + "From Timetable " + "inner join event "
                    + "on event.eventId=Timetable.eventId " + "inner join VolunteerJob "
                    + "on VolunteerJob.ID= Timetable.jobId " + "Where JobName='"
                    + jobarray[k] + "'" + "and Timetable.date ='" + date + "'"
                    + "and eventName='" + eventarray[k] + "'";
            // step 3 - to retrieve data using readRequest method
            rs = db.readRequest(dbQuery);

            try {
                if (rs.next()) {
                    tb.setName(namesarray[k]);
                    tb.setJobName(jobarray[k]);
                    tb.setEventName(rs.getString("eventName"));
                    tb.setDate(rs.getString("date"));
                    tb.setLocation(rs.getString("location"));
                    // **Adding the item retrieve into ArrayList called List**
                    list.add(k, tb);

                    System.out.println(list.get(k).getName() + " test234");
                }
            } catch (Exception e) {
                e.printStackTrace(); // fail to retrieve, print error message
            }

            // step 4 - close connection
            db.terminate();
        } // end of for loop**
        System.out.println(list.get(0).getName() + " outloop");
        System.out.println(list.get(1).getName() + " outloop");

        return list;
    }
}

Upvotes: 2

Views: 480

Answers (1)

Eran
Eran

Reputation: 393781

After your edit, I see that you created one instance of Timetable at the start of the method. That's not enough. It means you are adding the same object to the list over and over again, and you are overwriting its properties in each iteration.

You must create a new instance of Timetable for each object you add to the list.

    tb = new Timetable ();
    tb.setName(namesarray[k]);
    tb.setJobName(jobarray[k]); 
    tb.setEventName(rs.getString("eventName")); 
    tb.setDate(rs.getString("date"));
    tb.setLocation(rs.getString("location"));
    list.add(k, tb)

Upvotes: 4

Related Questions