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.