Ok cant seem to find the null pointer exception error on my code one moment its working fine the next when i add the 20 i get an error, and i dont know if this is what messing my find method.
Between my tree is supposed to be a binary non search tree, and its generic. The add methods works by randomly going left or right of current node. I hope my code is understendable. Any help is appreciated i had already made another post on how to start this tree, now i guess is about how to continue.

import java.util.*;
/**
 *
 * @author alejandro
 */
public class GenBinTree <T>
{
//btn is the binary tree node class
private static class btn<T>
{
    public T data;
    public btn<T> left;
    public btn<T> right;
    public btn(T data)
    {
        this.data=data;
        left=null;
        right=null;

    }


}
 //references the root of tree
     btn<T> root;
// creating a new tree with one node, the root
public GenBinTree()
{
    root=null;
}
//craeting a new tree with given root data
public GenBinTree(T rootData)
{
    root = new btn<> (rootData);
}
//checks if tree is empty
public boolean isEmpty()
{
    return root == null;
}
public boolean hasChildren(btn<T> someRoot)
{
    if(someRoot.left!=null&&someRoot.right!=null)
    {
        return true;
    }
    else 
    {
        return  false;
    }
}
//adds node to the tree with d being the data being passed
public void add(T d)
{
    if(this.isEmpty())
    {
        root = new btn<>(d);
    }
    else
    {
        add(d,root);
    }
}
public void add(T d,btn<T> treeRoot)
{
    //this will creat a random number of 1 or 0, mainly to randomly go left or right
    Random random = new Random();
    int x= random.nextInt(2);
  //randomly went to the left of current root  
   if(x==0)
   {    
        if(treeRoot.left ==null)
          treeRoot.left = new btn<>(d);


    //randomly going left or right
        else if(treeRoot.left !=null)
         {
            x=random.nextInt(2);
            if(x==0)
                  add(d,treeRoot.left);
            else
                  add(d,treeRoot.right);
         }
   }
   //randomly went to the right of current root
   else
   {
       if (treeRoot.right == null)
            treeRoot.right = new btn<>(d);

       else if(treeRoot.left !=null)
       {
          x=random.nextInt(2);
          if(x==0)
                add(d,treeRoot.left);
          else
              add(d,treeRoot.right);
       }
   }
}
/*public boolean find(T d)
{
    return(find(d,root));
}
public boolean find(T d,btn<T> n)
{
   boolean found = false;
   if(n.data==d)
   {
       return true;
   }

   else
   {
       find(d,n.left);
       find(d,n.right);
   }
   return found;

}
/*public void remove(T d) 
{
    if(root.data.equals(d))
    {
        root = null;
    }
    else
    {
         remove(d, root);
    }
}
public void remove(T d,btn<T> n)
{

    if(n.left==d&&hasChildren(n.left)==false)
    {
        n.left=null;
    }
    else if (n.right==d&&hasChildren(n.right)==false)
    {
        n.right=null;
    }
    else if(hasChildren(n.left)==true)
    {
        remove(d,n.left);
    }
    else
    {
        remove(d,n.right);
    }

}*/
public void Print(btn<T> n)
{
    if(n!=null)
    {
        Print(n.left);
        System.out.println(n.data);
        Print(n.right);

    }
}
public void Print()
{
    Print(root);
}


    public static void main(String[] args) 
    {
        int x;
        GenBinTree<Integer> l1 = new GenBinTree<>(10);
        l1.Print();
        System.out.println("************");
        l1.add(50);
        l1.add(20);
        l1.add(60);
        l1.Print();
        System.out.println("************");
        l1.add(50);
        l1.Print();
        System.out.println("************");
      //  System.out.println(l1.find(20));


    }
}

Recommended Answers

All 3 Replies

In your find() line 109, you didn't check whether the n is null but attempt to access it right away. It is possible to be null if it is a leave without data.

cool i think i see that bug thanks , but i think add() still has a bug when you run the program a few times am guessing theres a part in the tree with null pointer error heres the error it gives me it prints 10 then an error.

10
************
Exception in thread "main" java.lang.NullPointerException
    at genbintree.GenBinTree.add(GenBinTree.java:74)
    at genbintree.GenBinTree.add(GenBinTree.java:85)
    at genbintree.GenBinTree.add(GenBinTree.java:63)
    at genbintree.GenBinTree.main(GenBinTree.java:179)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

and i think its kind of hard to trace since its all random and stuff, but this happens like 1/7 runs not all the time. The rest of the time the tree prints fine:
Inorder printing:(when working fine)
10
************
50
20
10
60
************
50
20
10
50
60
************

Lines 81, 83, 95, and 98, they may be a problem there. If you are sending the left or right node of a known non-null node, you don't know whether the left and/or right is null? That may be the problem. You should add it right there if the selected branch is null.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.