0

The issue I'm having is that the get method throws NPE when the key is a different instance than the one in the TreeMap.

public class ConjuntDocuments {

private TreeMap<Capcalera, Document> almacen;
private ArrayList<Pair_plantilla> plantilla;

ConjuntDocuments() {
    almacen = new TreeMap<Capcalera, Document>(new CustomComparator());
    plantilla = new ArrayList<Pair_plantilla>();
}

private static class CustomComparator implements Comparator<Capcalera> {
    @Override
    public int compare(Capcalera c1, Capcalera c2) {
        int ax = c1.get_tit().get_nom().compareFrase(c2.get_tit().get_nom());
        if (ax < 0) return -1;
        else if (ax > 0) return 1;
        //titols iguals
        else {
            ax = c1.get_au().get_nom().compareFrase(c2.get_au().get_nom());
            if (ax < 0) return -1;
            else if (ax > 0) return 1;
        }
        //titols i autors iguals
        return 0;
    }
}

compareFrase compares ArrayLists(Paraula) -> Frase, Paraula is like a string, get_chars returns a String.

public int compareFrase(Frase f) {

    for(int i=0; i<min(this.get_size(), f.get_size()); ++i){
        int aux = this.get_paraula(i).get_chars().compareTo(f.get_paraula(i).get_chars());
        if(aux < 0) return -1;
        else if(aux > 0) return 1;
    }
    if(this.get_size() < f.get_size()) return -1;
    else if(this.get_size() > f.get_size()) return 1;
    return 0;
}

Titol and autor are Frases -> ArrayList(Paraula)

public class Capcalera {
private Titol tit;
private Autor au;

So after trying to figure this out, I've realised that the get method only works if the key referenced is the same instance than the one mapped, right after putting an entry (almacen.put(capcalera,document) , if I try to call almacen.get(Capcalera) it will return the value correctly, but if I create a new Capcalera, it will throw NPE. I'm assuming there is an issue with the comparator but since the entries are sorted correctly I can't figure out what is wrong.

EDIT:

I've implemented .equals and .hashcode from Capcalera, but I might be doing something wrong because .get from the Treemap still throws NPE.

@Override
public int hashCode() {
    int hashTitol = tit != null ? tit.hashCode() : 0;
    int hashAutor = au != null ? au.hashCode() : 0;

    return (hashTitol + hashAutor) * hashAutor + hashTitol;
}

@Override
public boolean equals(Object other) {
    if (other instanceof Capcalera) {
        Capcalera otherCapcalera = (Capcalera) other;
        return
                (( this.get_tit().get_nom().equalsFrase(otherCapcalera.get_tit().get_nom()) ||
                        ( this.get_tit() != null && otherCapcalera.get_tit() != null &&
                            this.get_tit().get_nom().equalsFrase(otherCapcalera.get_tit().get_nom()) )) &&
                        ( this.get_au().get_nom().equalsFrase(otherCapcalera.get_au().get_nom()) ||
                                ( this.get_au() != null && otherCapcalera.get_au() != null &&
                                    this.get_au().get_nom().equalsFrase(otherCapcalera.get_au().get_nom()))) );
    }
    return false;
}

equalsFrase returns true if Titol/Autor are equals

public boolean equalsFrase(Frase f) {
    for(int i=0; i<min(this.get_size(), f.get_size()); ++i){
        int aux = this.get_paraula(i).get_chars().compareTo(f.get_paraula(i).get_chars());
        if(aux < 0) return false;
        else if(aux > 0) return false;
    }
    if(this.get_size() < f.get_size()) return false;
    else if(this.get_size() > f.get_size()) return false;
    return true;
}
Adricu8
  • 9
  • 2
  • Which of these lines does the NPE come from? – Louis Wasserman Dec 12 '16 at 23:13
  • Can you show the stack trace? It will be helpful to know where the NPE came from. After reading the source code of `TreeMap`, it appears that the only way that `get()` can throw an NPE is if you pass `get()` a null key, and your comparator does not check for null keys (yours does not). Or, some property of your key (`Capcalera`) that is used in the comparison is null. I would suggest setting a breakpoint in your compare method and seeing if a null parameter is being passed in, or one of the properties used in the comparison is null. – StvnBrkdll Dec 15 '16 at 21:42

0 Answers0