OVH Cloud OVH Cloud

problème de cast à l'exécution

4 réponses
Avatar
yvon.thoravalNO-SPAM
B'jour !

J'ai un problème de cast à l'exécution donc non filtré par eclipse :

<code>
Set wines = new TreeSet();
for (int k = 0; k < vlColStr[i][j].length; k++) {
Set ceps = new TreeSet();
for (int l = 0; l < vlCepStr[i][j][k].length; l++) {
VinePlant cep = new VinePlant(vlCepStr[i][j][k][l]);
ceps.add(cep);//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<89
}
Wine wine = new Wine(vlColStr[i][j][k], vlTypStr[i][j][k], ceps);
wines.add(wine);
}
app.wines = wines;
</code>

la trace :
Exception in thread "main" java.lang.ClassCastException
at java.util.TreeMap.compare(TreeMap.java:1085)
at java.util.TreeMap.put(TreeMap.java:463)
at java.util.TreeSet.add(TreeSet.java:209)
at
yt.macave.appellations.datas.ValleeLoire.getValleeLoire(ValleeLoire.java
:89)
at
yt.macave.appellations.datas.ValleeLoire.main(ValleeLoire.java:112)

comme la trace va à compare, je suspecte donc mon code pour
VinePlant#compareTo :
<code>
public class VinePlant {
String name;

Integer scale;

public VinePlant() {
}

public VinePlant(String name) {
this.name = name;
this.scale = null;
}

public VinePlant(String name, int scale) {
this.name = name;
if (scale > 100)
scale = 100;
this.scale = new Integer(scale);
}

public boolean equals(Object that) {
if (this == that)
return true;
if (!(that instanceof Wine))
return false;
VinePlant thatVinePlant = (VinePlant) that;
return hasEqualState(thatVinePlant);
}

/**
* @param that
* is a non-null VinePlant.
*
* @throws NullPointerException
* if that is null.
* @throws ClassCastException
* if that is not an VinePlant object.
*/
public int compareTo(Object that) {
final int BEFORE = -1;
final int EQUAL = 0;
final int AFTER = 1;

// this optimization is usually worthwhile, and can
// always be added
if (this == that)
return EQUAL;

// Performing explicit checks for nullity and type are made
// redundant by the following cast, which will throw
// NullPointerException and ClassCastException in these
// respective cases.
final VinePlant thatVinePlant = (VinePlant) that;

int nameCompared = this.name.compareTo(thatVinePlant.name);
if (nameCompared < 0)
return BEFORE;
if (nameCompared > 0)
return AFTER;

int scaleCompared = this.scale.intValue()
- thatVinePlant.scale.intValue();
if (scaleCompared < 0)
return BEFORE;
if (scaleCompared > 0)
return AFTER;

return EQUAL;
}

public int hashCode() {
int result = HashCodeUtil.SEED;
result = HashCodeUtil.hash(result, this.name);
result = HashCodeUtil.hash(result, this.scale.intValue());
return result;
}

private boolean hasEqualState(VinePlant that) {
return (this.name == null ? that.name == null : this.name
.equals(that.name))
&& (this.scale.intValue() == 0 ? that.scale.intValue()
== 0
: (this.scale.intValue() ==
that.scale.intValue()));
}
}
</code>


--
yt

4 réponses

Avatar
Laurent Bossavit
Yvon,

Exception in thread "main" java.lang.ClassCastException
at java.util.TreeMap.compare(TreeMap.java:1085)


L'exception a été levée à la première ligne du stack trace; c'est donc
là qu'il faut aller pour savoir de quoi il retourne. Munis-toi du code
source des classes de l'API Java, et tu découvriras que les lignes 1084
et 1085 de TreeMap sont:

private int compare(Object k1, Object k2) {
return (comparator==null ? ((Comparable)k1).compareTo(k2)

On devine donc qu'une instance a été castée vers Comparable, qui
n'implémentait pas cette interface. Comme tu nous dis:

public class VinePlant {

on peut supposer que c'est cette classe qui doit implémenter Comparable.
Fournir une méthode compareTo() n'est pas suffisant.

Laurent

Avatar
Nico
"Yvon Thoraval" a écrit
public boolean equals(Object that) {
if (this == that)
return true;
if (!(that instanceof Wine))


Pouquoi pas instanceof VinePlant ?

return false;
VinePlant thatVinePlant = (VinePlant) that;
return hasEqualState(thatVinePlant);
}


Avatar
yvon.thoravalNO-SPAM
Nico wrote:


Pouquoi pas instanceof VinePlant ?


Merci beaucoup ! passe ke j'ai copié collé ça de wine vers vineplant...
arg )))
--
yt

Avatar
yvon.thoravalNO-SPAM
Yvon Thoraval wrote:

Merci beaucoup ! passe ke j'ai copié collé ça de wine vers vineplant...
arg )))


et ke, en +, j'avais oublié le " implements Comparable " arg )))
--
yt