In my Java application, I have a class MyFileList
that extends ArrayList and implements TableModel. The MyFile
class
When I click to sort a column in the gui, I receive an exception and the list is left half sorted.
Exception in thread "AWT-EventQueue-2" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.example.smdb.MyFileList.getValueAt(MyFileList.java:321)
at com.example.util.MyObjectListComparator.compare(MyObjectListComparator.java:97)
at java.util.TimSort.mergeHi(Unknown Source)
at java.util.TimSort.mergeAt(Unknown Source)
at java.util.TimSort.mergeForceCollapse(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.ArrayList.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at com.example.gui.MyTableSorter.sort(MyTableSorter.java:73)
at com.example.gui.MyTableSorter.sortByColumn(MyTableSorter.java:93)
at com.example.gui.PreservableColSizeJTable$PersistentJTableHeader.mouseClicked(PreservableColSizeJTable.java:935)
MyTableSorter
sort method:
public void sort(int column) {
if(model instanceof List){
Collections.sort ((List)model, new MyObjectListComparator((List)model, column, ascending));
}
}
MyObjectListComparator
compare method:
public int compare(Object obj, Object obj1) {
int result = 0;
int row = this.filelist.indexOf(obj);
int row1 = this.filelist.indexOf(obj1);
Object value = ((MyFileList) this.filelist).getValueAt(row, column);
value = ((MyFile.FileAttr) value).getAttrValue();
Object value1 = ((MyFileList) this.filelist).getValueAt(row1, column);
value1 = ((MyFile.FileAttr) value1).getAttrValue();
result = compareAttr(value, value1);
return result;
}
MyFile
equals method:
public boolean equals(Object obj) {
if (obj instanceof MyFile) {
if (this.getAttribute(FILE_ID) != null) {
return this.getAttribute(FILE_ID).equals(((MyFile) obj).getAttribute(FILE_ID));
}
}
return false;
}
Upon debugging, I see that this.filelist.indexOf(obj)
in the compare
method is returning -1
. If I catch this exception and let it keep sorting, I end up with some items duplicated in my list and some items missing.
I found some references to a bug in TimSort
that sound very similar, so I tried the useLegacySortMerge
option. That gives me the same behavior with a slightly different stack trace, so unfortunately that doesn't look like the issue.
Exception in thread "AWT-EventQueue-2" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.example.smdb.MyFileList.getValueAt(MyFileList.java:321)
at com.example.util.MyObjectListComparator.compare(MyObjectListComparator.java:97)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.legacyMergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.ArrayList.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at com.example.gui.MyTableSorter.sort(MyTableSorter.java:73)
at com.example.gui.MyTableSorter.sortByColumn(MyTableSorter.java:93)
at com.example.gui.PreservableColSizeJTable$PersistentJTableHeader.mouseClicked(PreservableColSizeJTable.java:935)
This has been driving me crazy for a few days. Any insights?