2

I am implementing a BST which has 3 classes: BST, BSTNode and Profile.

BST class

public class BST {

    private static BSTNode root;
    private BSTNode parent;

    BST() {
        root = null;
    }

    public void insertProfile(Profile p) {

        BSTNode newNode = new BSTNode(p);

        if (root == null) {
            root = newNode;
        } else {
            BSTNode focusNode = root;

            BSTNode parent = null;
            int compare = focusNode.getProfile().getName().compareTo(parent.getProfile().getName());
            while (true) {
                parent = focusNode;
                if (compare < 0) {
                    focusNode = focusNode.left;
                    if (focusNode == null) {
                        parent.left = newNode;
                        return;
                    } else {
                        focusNode = focusNode.right;
                        if (focusNode == null) {
                            parent.right = newNode;
                            return;
                        }
                    }
                }
            }
        }

    }


}

When I add a single entry to the BST, it seems to work fine, but then when I add two or more entries to the BST, it gives me this error:

Exception in thread "main" java.lang.NullPointerException
    at BST.insertProfile(BST.java:21)
    at BSTMain.main(BSTMain.java:19)

I have been trying to resolve this for many hours, if anyone could hint where I am going wrong, would be very appreciative.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – avojak May 11 '17 at 18:39

2 Answers2

0

It's because of this code (in BST.java, right after the else statement in insertProfile:

BSTNode parent = null;
int compare = focusNode.getProfile().getName().compareTo(parent.getProfile().getName());

What you're doing is declaring that parent is null, but then calling a method (getProfile) on it, which is what is causing the NullPointerException. You have to instantiate parent first.

Aneesh Ashutosh
  • 762
  • 7
  • 18
  • Thanky you very much! If I change too: BSTNode parent = new BSTNode(p); would it work? – Rhodri Jones May 11 '17 at 18:44
  • You'd have to make some more changes to get your code to work; for example, the `else` statement in your `while` loop is misplaced (it should be after the first `if` statement, but instead is after the second). Additionally, you want to move the statement where you evaluate `compare` inside the `while` loop as well, so that the loop can terminate. Something that helps me is (physically) drawing out each step of the process and seeing how I would code it; you might be able to see a couple of the other mistakes made within the `while` loop that way. – Aneesh Ashutosh May 11 '17 at 18:49
0

Correct insert BST to balance nodes and surround nullpointers.

public static Node insert(Node root,int data) {

    if(root == null){
        Node node = new Node(data);
        root = node;
    }else if (data > root.data) {
        root.right = insert(root.right , data);
    }else if (data < root.data){
        root.left = insert(root.left , data);
    }

    return root;
}
edmarr
  • 23
  • 4