It shows null pointer exception in Driver class, can anybody find me the error ??
public class Node {
int iData;
double dData;
Node leftChild;
Node rightChild;
public void displayNode(){
System.out.println(dData);
}
}
public class Tree {
private Node root; //the root of the tree
public Node find(int key) // find node with given key
{ // (assumes non-empty tree)
Node current = root; // start at root
while(current.iData != key) // while no match,
{
if(key < current.iData) // go left?
current = current.leftChild;
else
current = current.rightChild; // or go right?
if(current == null) // if no child,
return null; // didn't find it
}
return current; // found it
}
public void insert(int id, double dd){
Node newNode = new Node(); // make new node
newNode.iData = id; // insert data
newNode.dData = dd;
if(root==null) // no node in root
root = newNode;
else // root occupied
{
Node current = root; // start at root
Node parent;
while(true) // (exits internally)
{
parent = current;
if(id < current.iData) // go left?
{
current = current.leftChild;
if(current == null) // if end of the line,
{ // insert on left
parent.leftChild = newNode;
return;
}
} // end if go left
else // or go right?
{
current = current.rightChild;
if(current == null) // if end of the line
{ // insert on right
parent.rightChild = newNode;
return;
}
} // end else go right
} // end while
} // end else not root
} // end insert()
public void delete(int id){}
//various other methods
public void inOrder(){ //utility function
inOrderRead(root);
}
private void inOrderRead(Node localRoot){
while(localRoot != null){
inOrderRead(localRoot.leftChild);
localRoot.displayNode();
inOrderRead(localRoot.rightChild);
}
}
}
It shows here null pointer exception !
public class TreeApp {
public static void main(String[]args){
Tree theTree = new Tree();
theTree.insert(50, 1.5);
theTree.insert(41, 50.0);
theTree.insert(11, 0.0);
theTree.inOrder();
}
}
i think something wrong with my functions,, please help its very urgent!!