user1947288
user1947288

Reputation: 33

Objects getting copied by reference and replacing all items in ArrayList

I understand that people have faced this issue before and I have gone through the previous posts. I have an arrayList and I'm trying to add objects to it. During every add call, the same object reference is being copied. Though I have used the 'new' operator and am creating new objects. This is something basic and has worked previously when I create the object again during each iteration. Any help is much appreciated. Here is my code.

public List<Actor> readAllActors()
{
    String selectMovie = "SELECT * from ACTOR;";
    List<Actor> listOfActors = new ArrayList<Actor>();
    try {           
        statement = conn.prepareStatement(selectMovie);
        results = statement.executeQuery(selectMovie);      
                    Actor a = new Actor();
        while (results.next())
        {                  
                       a = getActorFromResult(results);
                       listOfActors.add(new Actor(a.getId(), a.getFirstName(), a.getLastName(), a.getDateOfBirth()));                               
        }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
    return listOfActors;    
}

private Actor getActorFromResult(ResultSet results) throws SQLException {
    // TODO Auto-generated method stub

    int id = results.getInt("id");
    String fname = results.getString("firstName");
    String lname = results.getString("lastName");
    String dob = results.getString("dateOfBirth");

    Actor actor = new Actor(id, fname, lname, dob );        
    return actor;
}

I have tried printing the object in each iteration. It is fetching the right row from the table.. the new Actor() seems to have no effect in creating a new object reference!!

Upvotes: 1

Views: 95

Answers (2)

Zhedar
Zhedar

Reputation: 3510

Your code is fine and can't pass the same reference all the time. You can check it via a == check of your listed objects.

You could refactor you code to

results = statement.executeQuery(selectMovie);      
while (results.next())
{          
    listOfActors.add(getActorFromResult(results));                    
}

as you're creating a new Actor and thus a new object with an own reference after fetching the result set. This will also make the code clearer here.

Also return new Actor(id, fname, lname, dob ); will do the job in your result method. You're not using the self-descriptive local variable anyways.

I suspect your problem lies in your database. Try querying it with a database tool.

Upvotes: 1

RMachnik
RMachnik

Reputation: 3684

try to change that line

listOfActors.add(new Actor(a.getId(), a.getFirstName(), a.getLastName(), a.getDateOfBirth()));

to that

listOfActors.add(getActorFromResult(results));

and delete line

a = getActorFromResult(results);

Your references are for sure not the same, please check it in debugger.

Upvotes: 0

Related Questions