How to avoid ClassCastException when TableRowSorter hits null values?

Hi,

I just have my own Table-Modell

protected String[] rowNames;

protected Number[][] values;

Number is just for subclasses to store any type like Long or Double.

I have often just some empty cols in the Table to make the content more human readable, but this forces null-Vales in the values-Array.

(Should be no Problem cause TableModel don't makes any Assumptions about the Data-Structure.)

If I sort any Number-Row with null values this exception is raised:

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String

at java.lang.String.compareTo(Unknown Source)

at javax.swing.table.TableRowSorter$ComparableComparator.compare(Unknown Source)

at javax.swing.DefaultRowSorter.compare(Unknown Source)

at javax.swing.DefaultRowSorter.access$100(Unknown Source)

at javax.swing.DefaultRowSorter$Row.compareTo(Unknown Source)

at javax.swing.DefaultRowSorter$Row.compareTo(Unknown Source)

at java.util.Arrays.mergeSort(Unknown Source)

at java.util.Arrays.mergeSort(Unknown Source)

at java.util.Arrays.mergeSort(Unknown Source)[ERROR] [AWT-EventQueue-0]"2007-04-19 10:19:06,843" util.HandleAllExceptionOfEventDispatching

I have also overwritten

@Override

public Class<?> getColumnClass(finalint col ){

if( col > 0){

return this.values.getClass();

}

return String.class;

}

I can't believe that null values are causing this trouble, cause they are normally quite common and TableRowSorter should care by default about them. But it uses e.g. Long.compareTo (according to API) which isn't aware of null-Values:

/**

* Compares two <code>Long</code> objects numerically.

*

* @paramanotherLongthe <code>Long</code> to be compared.

* @returnthe value <code>0</code> if this <code>Long</code> is

* equal to the argument <code>Long</code>; a value less than

* <code>0</code> if this <code>Long</code> is numerically less

* than the argument <code>Long</code>; and a value greater

* than <code>0</code> if this <code>Long</code> is numerically

* greater than the argument <code>Long</code> (signed

* comparison).

* @since1.2

*/

publicint compareTo(Long anotherLong){

long thisVal = this.value;

long anotherVal = anotherLong.value;

return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));

}

even a Null-Pointer-Exception should be more likely than a ClassCastException.

So may be it used String comparision as fall-back-option?

Copied from source-file.

So do I have to set for each class-Type my own Comparator or is there an easier way to do that?

Thanks in advance.

Greetings Michael>

[3611 byte] By [Urmecha] at [2007-11-27 1:35:19]
# 1

Hi,

in the meantime I tried this and it works:

Comparator<Long> longComp = new Comparator<Long>(){

public int compare(Long o1, Long o2) {

if(o1 == null && o2 == null){

return 0;

}

if(o1 == null && o2 != null){

return -1;

}

if(o1 != null && o2 == null){

return 1;

}

return o1.compareTo(o2);

}

};

for(int i = 0, n = model.getColumnCount(); i < n; i++){

if(model.getColumnClass(i).equals(Long.class) ){

sorter.setComparator(i, longComp);

}

}

So the null-values are the real cause of the ClassCastException.

But cause Number don't implements Comparable, I have to do this for any subclass I use (e.g. Integer, Float, Double) . Is there any better solution to do this?

Tanks a lot.

Greetings Michael

Urmecha at 2007-7-12 0:43:39 > top of Java-index,Desktop,Core GUI APIs...
# 2
Bumping
Urmecha at 2007-7-12 0:43:39 > top of Java-index,Desktop,Core GUI APIs...