0

I am creating a JavaFXML application and I need to have these classes there: ListOfAllEvents, Events and Instructors. ListOfAllEvents has an ArrayList of Events, and each Event has an ArrayList of Instructors.

Now, clicking on buttons and using methods for creating instances I first create several Instructors and add them to the ArrayList CurrentInstructors, then I create an instance of Event, assign the list of instructors to it and then I would like to add my new Event to the ArrayList CurrentEvents.

My problem is, CurrentEvents does add only one Event, and when I call the method for creating new Event for second time, the second event replaces the first one, even though my CurrentInstructors work correctly.

Can you help me? I am a beginner at Java, so I'll be thankful for every piece of advice.

My code:

 public class FXMLDocumentController implements Initializable {
(...)
 private ArrayList<Instructor> CurrentInstructors = new ArrayList<Instructor>();
private ArrayList<Event> CurrentEvents = new ArrayList<Event>();

@FXML
private void AddInstructor_Click(ActionEvent event) {
    TextInputDialog dialog = new TextInputDialog("");

    Optional<String> result = dialog.showAndWait();
    if (result.isPresent()){
        Instructor newInstructor = new Instructor(result.get());
        CurrentInstructors.add(newInstructor);

    }
}


private void NewEvent () {
Event newEvent = new Event(EventName.getText());

//this is the problematic row:
CurrentEvents.add(newEvent);

newEvent.setInstructors(CurrentInstructors);

CurrentInstructors.clear();

}
(...)
}

Instructor class:

public class Instructor {
private String name;

public String getName() {
    return name;
}
public Instructor(String name){
    this.name = name;

}

}

Event Class:

public class Event {
private String name;
private ArrayList<Instructor> Instructors = new ArrayList<Instructor>();

 public Event(String name){
    this.name = name;
}
//getters, setters

}
  • 2
    You will want to create and post a valid [mcve] for your best chances of getting a decent answer. This is not your entire program but rather a new **small** program, small enough to post here in its entirety, one that demonstrates the problem for us. – Hovercraft Full Of Eels Sep 17 '17 at 20:33
  • 3
    Stop using `CurrentInstructors.clear();` if you dont want elements to be deleted... – OneCricketeer Sep 17 '17 at 20:36
  • Welcome to Stack Overflow! Please take the [tour](/tour), have a look around, and read through the [help center](/help), in particular [How do I ask a good question?](/help/how-to-ask) and [What topics can I ask about here?](/help/on-topic). Please read and follow the [Java Naming Conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html) – Timothy Truckle Sep 17 '17 at 20:47

1 Answers1

2

You only have one list which you are setting, then immediately emptying.

newEvent.setInstructors(CurrentInstructors);
CurrentInstructors.clear(); // This clears the list just set to newEvent

Since Java is pass-by-value, you have multiple references to a single List object

If you want each new event to hold it's own list, you need to explicitly make new ArrayList<>() each newEvent object made, then add to that, and call setInstructors

For example,

private void newEvent(String name) {
    Event newEvent = new Event(name);
    // Copy the list
    List<Instructor> instructors = new ArrayList<>(currentInstructors);
    // Set that list
    newEvent.setInstructors(instructors);
    currentEvents.add(newEvent);
    // Now clearing this list won't clear the newEvent list
    currentInstructors.clear();
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245