NullPointerException lors du tri d'une map
:salut:
J'ai un problème lors du tri de ma map par valeur, je tombe sur une exception NullPointerException et je n'arrive pas à voir où ce trouve mon erreur.
Voici le code que j'utilise :
Code:
1 2 3 4 5 6 7 8 9
| public void display () {
List<String> keys = new ArrayList<> (this.list.keySet ());
Collections.sort (keys, new ValueComparator (this.list));
for (String str : keys){
System.out.println ("string : " + str + " count : " + this.list.get (str));
}
} |
Ma classe implémentant l'interface Comparator :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class ValueComparator implements Comparator<String> {
private Map<String, Integer> map;
public ValueComparator (Map<String, Integer> map) {
this.map.putAll (map);
}
@Override
public int compare (String sourceKey, String targetKey) {
int sourceValue = Integer.valueOf (this.map.get (sourceKey));
int targetValue = Integer.valueOf (this.map.get (targetKey));
if (sourceValue > targetValue) {
return 1;
}
else if (sourceValue > targetValue) {
return -1;
}
return 0;
}
} |
Une idée de ce que j'aurai fait de traver ?
:merci: par avance :)