Skip to content

Commit

Permalink
bug: fix NullPointerException in optional fields
Browse files Browse the repository at this point in the history
Source.getEmail() returns a null, using toString() on a null causes the Exception.
Since Optional.ofNullable only handles the null cases within its parameters, to handle null pointer safely, convert it to string outside of ofNullable()
Remove Optional Values in Person.equals() to avoid null pointer
  • Loading branch information
jovantanyk committed Mar 20, 2024
1 parent 9436d09 commit 20dee78
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ public boolean equals(Object other) {
&& phone.equals(otherPerson.phone)
&& address.equals(otherPerson.address)
&& dateOfBirth.equals(otherPerson.dateOfBirth)
&& dateOfAdmission.equals(otherPerson.dateOfAdmission)
&& sex.equals(otherPerson.sex);
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ public JsonAdaptedPerson(Person source) {
this.dateOfBirth = source.getDateOfBirth().toString();
this.sex = source.getSex().toString();
this.status = source.getStatus().toString();
this.email = Optional.ofNullable(source.getEmail().toString());
this.country = Optional.ofNullable(source.getCountry().toString());
this.allergies = Optional.ofNullable(source.getAllergies().toString());
this.bloodType = Optional.ofNullable(source.getBloodType().toString());
this.condition = Optional.ofNullable(source.getCondition().toString());
this.dateOfAdmission = Optional.ofNullable(source.getDateOfAdmission().toString());
this.diagnosis = Optional.ofNullable(source.getDiagnosis().toString());
this.symptom = Optional.ofNullable(source.getSymptom().toString());
this.email = Optional.ofNullable(source.getEmail()).map(Email::toString);
this.country = Optional.ofNullable(source.getCountry()).map(Country::toString);
this.allergies = Optional.ofNullable(source.getAllergies()).map(Allergies::toString);
this.bloodType = Optional.ofNullable(source.getBloodType()).map(BloodType::toString);
this.condition = Optional.ofNullable(source.getCondition()).map(Condition::toString);
this.dateOfAdmission = Optional.ofNullable(source.getDateOfAdmission()).map(DateOfAdmission::toString);
this.diagnosis = Optional.ofNullable(source.getDiagnosis()).map(Diagnosis::toString);
this.symptom = Optional.ofNullable(source.getSymptom()).map(Symptom::toString);
}

/**
Expand Down

0 comments on commit 20dee78

Please sign in to comment.