I created my own exception class InvalidHallOfFameIDException. Basically what i’m doing is looping through a arraylist called hallOfFameMemberList that holds hall of fame members. Inside that loop i am looping through Hall Of Fames, which members can belong to. An exception(InvalidHallOfFameIDException) is thrown when a member’s id from hallOfFameMemberList does not equal to the Hall Of Fame’s id. The problem I am having is after the exception is thrown. After the exception is thrown I want to continue looping through hallOfFameMemberList. It goes to the next hofMember of hallOfFameMemberList then gives me this error:
java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at ReportWriter.displayReport3(HW6.java:1708)
at ReportWriter.DisplayReports(HW6.java:1478)
at HW6.main(HW6.java:399)
Here is my code
/*loop to check and delete member from arraylist if member hall of fame id does not equal to hall of fame id*/
for(HallOfFameMember hofMember: hallOfFameMemberList){
memberFound = false;
try{
for(HallOfFame hof:hallOfFameArr){
if(hofMember.GetHallOfFameId() == hof.GetHallOfFameId()){
/*do something then set memberFound = true*/
}
}
if(memberFound == false){
/*remove member from list*/
hallOfFameMemberList.remove(hofMember);
throw new InvalidHallOfFameIDException();
}
}catch(InvalidHallOfFameIDException e){
hallOfFameMemberList.remove(hofMember);
System.err.println("InvalidHallOfFameIDException was thrown:");
System.err.println("HallOfFameMember " + hofMember.GetLastName() + ", " + hofMember.GetFirstName() + " with HOFId " + hofMember.GetHallOfFameId() + " was deleted");
}
I don’t quite understand the error. Any ideas on what I can do?