This is probably very simple but i'm not seeing how to do it.
My GUI for a project reads objects from a flat file (substitution for database) and puts each object as an item in a jComboBox. Then if the user wants to delete one of the objects, he selects the object in the Combo Box and presses the delete button. Here is the code for the delete button action listener:
private void DeleteAssignmentButtonActionPerformed(java.awt.event.ActionEvent evt) {
AssignmentParser parse = new AssignmentParser();
Assignment[] existing = parse.getAllAssignments();
int index = AssignmentToManage.getSelectedIndex();
Assignment toDelete = existing[index];
AssignmentToManage.setSelectedIndex(0);
parse.deleteAssignment(toDelete.getID());
existing = parse.getAllAssignments();
for(int i = 0; i < existing.length; i++)
{
AssignmentToManage.removeItemAt(i);
AssignmentToManage.insertItemAt(existing[i].getAssignmentName(), i);
}
}
However, when I try to delete, my combo box action listener throws an exception:
run:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
at gist.mainUI.AssignmentToManageActionPerformed(mainUI.java:1573)
at gist.mainUI.access$1700(mainUI.java:28)
at gist.mainUI$22.actionPerformed(mainUI.java:742)
then goes up the family tree for combo box. The error is occurring in the action listener for the combo box:
private void AssignmentToManageActionPerformed(java.awt.event.ActionEvent evt) {
AssignmentParser parse = new AssignmentParser();
Assignment[] existing = parse.getAllAssignments();
int index = AssignmentToManage.getSelectedIndex();
Assignment toManage = existing[index];//This line is the error
int possible = toManage.getTotalPoints();
AssignmentOutOfLabel.setText("Maximum Points: " + possible);
}
Also, it adds additional instances of the last object of the combobox to the bottom(ie if hw1 is the last value in the combobox it adds another hw1)