0

I have created BinarySearch Tree and I have a problem inserting key and values. It says NullPointerException.

package BST;

public class Tree 
{
    public Node root = null;

    public void insert(int key, Object obj)
    {
        Entry entry = new Entry(key,obj);
        if(this.root==null)
        {
            this.root = new Node(entry, null);
        }
        else
        {
            insert(entry,this.root); //error
        }
    }
    public void insert(Entry entry, Node temp)
    {
        if(((Entry) temp.getObj()).getKey() > entry.getKey()) //error
        {
            if(temp.isLeft())
                insert(entry, temp.childLeft());
            else
                temp.addLeft(entry);
        }
        else
        {
            if(temp.isRight())
                insert(entry, temp.childRight());
            else
                temp.addRight(entry);
        }
    }//insert

    public void inorderT()
    {
        inorderT(this.root);
    }
    private void inorderT(Node t)
    {
        if(t.isLeft())
            this.inorderT(t.childLeft());
        ((Entry)t.getObj()).printEntry();
        if(t.isRight())
            this.inorderT(t.childRight());
    }

    public void find(int key)
    {
        System.out.println("키 값이" + key + " 인 엔트리 출력하기");
        find(key,root);
    }
    private void find(int key, Node temp)
    {
        Entry entry = ((Entry)temp.getObj());

        if(entry.getKey()==key)
        {
            entry.printEntry();
            return;
        }
        if(temp.isLeaf())
        {
            System.out.println("찾기 실패");
            return;
        }

        if(entry.getKey()==key)
        {
            find(key, temp.childLeft());
        }
        else
        {
            find(key, temp.childRight());
        }
    }
}

The sentences that say //error is the problem I have. I totally don't know why it's not working BTW, I am so newbie at Java language. So don't blame me if it is a stupid question :)

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
Gom
  • 3
  • 1
  • I would breakpoint that line of code in your debugger and see which expression is `null` and try to work out why. I would also try to find the simplest test where this happens to make it easier to work out why you are getting this error. – Peter Lawrey May 22 '14 at 11:35
  • 3
    Do you understand what a null-pointer exception means? – Evan Knowles May 22 '14 at 11:36
  • On which line number you are getting error for above code? – Ketan Bhavsar May 22 '14 at 11:40

2 Answers2

0

replace

if(((Entry) temp.getObj()).getKey() > entry.getKey()) //error

with

if( temp.getObj())!=null && temp.getObj()).getKey()!=null && ((Entry) temp.getObj()).getKey() > entry.getKey()) //solution
Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
0

In method public void insert(int key, Object obj) if root is null u are creating

    this.root = new Node(entry, null);

And then if you pass this.root to public void insert(Entry entry, Node temp) you may experience NullPointerException in line you marked as a error, because temp.getObj() returns null, and then u are trying to invoke getKey() on null object.

Kasper Ziemianek
  • 1,329
  • 8
  • 15