I write method to find node of student records by the first and last name of student in Binary Search Tree
This is the code of the method in class Binary Search Tree
private Student root;
public Student findNodeName(String firstname,String lastname) {
return findNodeName(root,firstname,lastname);
}
private Student findNodeName(Student student,String firstname,String
lastname){
if (student == null) {
return null;
}
else if (firstname.equals(student.getFirstName())
&&lastname.equals(student.getLastName())) {
return student;
}
else
{
if(student.getLeft()!= null)
return findNodeName(student.getLeft(),firstname,lastname);
else
return findNodeName(student.getRight(),firstname,lastname);
}
}
And This code in main class
//declare object of class universitydbmsBST
universitydbmsBST universitydbmsbst;
if (command.equals("SEARCHNAME") == true) {
//variables to read from input file
String firstname = input.next();
String lastname = input.next();
//variable used to check if the student is already in BSt or not
Student student =null;
//print if the student found or not
if((student =
universitydbmsbst.findNodeName(firstname,lastname))!=null){
output.println("SEARCHNAME Command");
output.println("Found: ID "+student.getID()+",
"+student.getFirstName()+" "+student.getLastName());
}
else {
output.println("SEARCHNAME Command");
output.println(student.getFirstName()+"
"+student.getLastName()+" was not found in
FCITbook.");
}
}
But I have a problem when I do run it appears NullPointerException
Can anyone help me?