0

I made a code for Binary Search Tree. It has only Insert function. I am getting an error at insert() function's while condition.

Exception in thread "main" java.lang.NullPointerException
    at tree.LinkNode.insert(BST.java)
    at tree.BST.main(BST.java)

I have initialised the Dynamic variable stub in the insert() function using keyword new.

Where did I go wrong?

The code has three class:

  1. BST (contain the main() class)
  2. LinkNode (contains LinkNode references and the insert() function)
  3. MyLinkNode (A class to initialise the root node)

Code:

public class BST {

    public static void main(String[] args){
    MyLinkNode start = new MyLinkNode(42);
    LinkNode node = new LinkNode();
    node.insert(start.root, 36);
    }
}

class LinkNode{

    LinkNode leftnode;
    LinkNode rightnode;
    LinkNode parentnode;
    int value;


    public LinkNode() {}


    public String insert(LinkNode root, int val){
        LinkNode stub = new LinkNode();
        stub = root;
        while(stub.leftnode == null || stub.rightnode == null){
            if(stub.value < val){
                stub = stub.leftnode ;
            }
            else if(stub.value > val){
                stub = stub.rightnode;
            }
            else{
                System.out.println("You Cannot insert a value that already exist in the tree.\nPlease insert a different value");
                return "";
            }
        }
        stub.value = val;
        stub.leftnode = null;
        stub.rightnode = null;
        this.parentnode = stub;
        return "Insertion Succesful";
    }
}

class MyLinkNode{

    LinkNode root;

    public MyLinkNode(int val){
        root = new LinkNode();
        root.value = val;
        root.parentnode = null;
        root.leftnode = null;
        root.rightnode = null;
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
HarshvardhanSharma
  • 754
  • 2
  • 14
  • 28
  • 1
    A debugger will tell you faster than SO will. You have a null reference. The stack trace tells you the file and the line. That should be enough to figure it out. – duffymo Jan 13 '17 at 13:16
  • @AndrewTobilko I have read that if I instantiate dynamic var `root` using `new` then it should not be null. – HarshvardhanSharma Jan 13 '17 at 13:34
  • That is true, `new` will never give you `null`. – Ole V.V. Jan 13 '17 at 13:42
  • This condition doesn’t look sound at first glance: `while(stub.leftnode == null || stub.rightnode == null){`. – Ole V.V. Jan 13 '17 at 13:45
  • @OleV.V. I am not very sure about the logic involved, after the assignment `stub = root`, won't `stub.leftnodel`, `stub.rightnode`, `stub.parentnode` all become `null` and `stub.value = 42`. so I Used `while(stub.leftnode == null || stub.rightnode == null)`. How is it wrong conceptually that it throws `NullPointerException` – HarshvardhanSharma Jan 13 '17 at 14:11
  • You are correct. Since 42 is greater than `val` (which is 36), in your while loop you are setting `stub` to `stub.rightnode`, that is, to `null`. So when checking whether to go once more through the loop, you first look at `stub.leftnode`, but since `stub` is now `null`, this triggers your `NullPointerException`. Hope this answered your question or at least got you in the right direction. – Ole V.V. Jan 13 '17 at 14:48
  • @OleV.V. Thank for the explanation, that made it clear. – HarshvardhanSharma Jan 13 '17 at 16:07

0 Answers0