简体   繁体   中英

Huffman Tree Compare NullPointerException

I have a project from my university teacher requiring comparing two alphabetic Huffman Trees. We should compare smallest char on trees.

This is our HuffmanNode class and we can't change this. The test class uses it (we can't see this class because we deliver the project on bilmoodle).

public class HuffmanNode {
    public Character value; // Karakter
    public int frequency; // Frekansı
    public HuffmanNode left; // sol çocuk
    public HuffmanNode right; // sağ çocuk

    public HuffmanNode(Character value, int frequency, HuffmanNode left, HuffmanNode right) {
        this.value = value;
        this.frequency = frequency;
        this.left = left;
        this.right = right;
    }
}

find method finds and returns the smallest char in the tree.

compare method compares 2 tree's smallest chars and should return 0, 1, -1.

class AlfabetikKarsilastirici implements Comparator<HuffmanNode> {
    public  int find(HuffmanNode o){
        if(o.left.value != null && o.right.value != null){
            if (o.left.value.compareTo(o.right.value)>0)
                return o.right.value;
            else {return o.left.value;}
        }
        else if (o.left.value != null)
            find(o.left);
        else
            find(o.right);
        return 0;
    }
    @Override
    public int compare(HuffmanNode o1, HuffmanNode o2) {
        int o1F=find(o1);
        int o2F=find(o2);
        if(o1F>o2F)
            return 1;
        else if(o1F<o2F)
            return -1;

        return 0;
    }
}

I encountered the below error and can't find where is the problem. My question is how can I resolve this error?

java.lang.NullPointerException
at AlfabetikKarsilastirici.find(Odev3Ogrenci.java:29)
at AlfabetikKarsilastirici.find(Odev3Ogrenci.java:35)
at AlfabetikKarsilastirici.find(Odev3Ogrenci.java:37)
at AlfabetikKarsilastirici.find(Odev3Ogrenci.java:37)
at AlfabetikKarsilastirici.compare(Odev3Ogrenci.java:42)

I am suffering same error.Probably you are studying in Pamukkale University

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM