I am trying to run my Binary Search Tree, I am creating objects of type Employee in my main program which does not seem to give me problems, but when I choose to search for an item in my BST, the program is terminated.
System.out.println("Searching the Binary Search Tree");
System.out.println("Enter surname to search for:");
String choice2 = sc.nextLine();
BinaryNode a = temp.search(choice2);
Employee newEmp = (Employee) a.obj;
if (a == null)
{
System.out.println("Not Found");
}
else
{
System.out.println(newEmp.getData());
}
break;
}
When the program terminates, it points to the line
Employee newEmp = (Employee) a.obj;
and the error given is, java.lang.NullPointerException: null
Could anyone tell me why this is happening please?
You can not access to .obj of null object.
Line should be moved after check whether a==null.