nus-cs2103-AY2223S2 / forum

12 stars 0 forks source link

Problem with FXCollections.observableArrayList(), setAll in uniquepersonlist.java #289

Closed sumhungyee closed 1 year ago

sumhungyee commented 1 year ago

image persons is a type List\<Person>, internalList is of type ObservableList\<Person> setAll clears the list instead of setting everything. somehow only does this for persons not integers pls send help prof! :( @damithc

damithc commented 1 year ago

@sumhungyee Perhaps you can give the code as text instead of screenshots so that others can easy copy-paste-modify the code?

sumhungyee commented 1 year ago
public void setPersons(List<Person> persons) {
        requireAllNonNull(persons);

        ObservableList<Person> test = FXCollections.observableArrayList();
        Person p = new Person(new Name("a"), new Postal("111111"), new Date("2000-01-01"), new Age("11"),  getVariantSet("DENV1"));
        test.setAll(List.<Person>of(p));
        System.out.println(test);
        test.setAll(List.<Person>of(p));
        System.out.println(test);

        if (!personsAreUnique(persons)) {
            throw new DuplicatePersonException();
        }

        System.out.println("UNIQUEPLIST: " + persons);
        System.out.println("in: " + this.internalList);
        this.internalList.setAll(persons);
        System.out.println(this.internalList);
    }
Thanks prof. Also, somehow when changing from ObservableList<Integer> to ObservableList<Person> the test code (above) works now, but the main setAll code still fails
sumhungyee commented 1 year ago

Resolved. Issue was persons is mutable. Replaced with List.copyOf(persons)

damithc commented 1 year ago

Resolved. Issue was persons is mutable. Replaced with List.copyOf(persons)

The so called 'rubber-duck debugging' :-p i.e., trying to explain the problem in details sometimes leads to the solution.