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 :)