The purpose of my assignment is to generate a report based on data read in from a file. The file is properly read in, and the values are calculated correctly. However, one of my linked lists, StudentList, throws a NullPointException that's marked at the getCourse() at other times, and I'm not sure of how to actually solve it. Below will be the code for the StudentNode class getCourse() comes from and the specific linked list. The class StudentNode is originally in a separate file from the insert method. Ultimately, I'm not understanding what I should do with my getCourse() so that the NPE is not thrown, and so I can still use that method to access the insert method to add a node.
public class StudentNode {
private String lastName; //holds lastName
private String firstName; //holds firstName
private String id; //holds identification number
private CourseList course; //allows access to traverse method for printing all classes of a student
private StudentNode next; //self referential variable
public StudentNode(){
//Precondition: must be called via object creation
//Postcondition: sets values of lastName, firstName, id and course to empty strings, and gpa to 0.0
new StudentNode("", "", "");
}
public StudentNode(String lastName, String firstName, String id){
//Precondition: must be called via object creation with correct parameters
//Postcondition: sets values of lastName, firstName, id, course and gpa to values passed in parameter
this.setLastName(lastName);
this.setFirstName(firstName);
this.setId(id);
}
public void setLastName(String lastName) {
//Precondition: passed variable is a String
//Postcondition: this.lastName is set to value of lastName
this.lastName = lastName;
}//end setLastName
public String getName() {
//Precondition: none
//Postcondition: returns firstName
return this.lastName + " " + this.firstName;
}
public void setFirstName(String firstName) {
//Precondition: passed variable is a String
//Postcondition: sets this.firstName to value of firstName
this.firstName = firstName;
}
public String getId() {
//Precondition: none
//Postcondition: returns id
return this.id;
}
public void setId(String id) {
//Precondition: passed variable is a String
//Postcondition: this.id is set to value of id
this.id = id;
}
public void setNext(StudentNode node){
this.next = node;
}
public StudentNode getNext(){
return this.next;
}
public String getLastName() {
return this.lastName;
}
public String getFirstName() {
return this.firstName;
}
public CourseList getCourse() {
return this.course;
}
public void setCourse(CourseList course) {
this.course = course;
}
public void printStudent(){
System.out.printf("%20s%10s\n", getName(), this.getId());
course.traverse();
}
}
public void insert(StudentNode node, CourseNode newCourse){
if (head == null){
head = node;
}
else {
boolean inserted = false;
StudentNode curr = head;
StudentNode prev = null;
while (curr.getNext() != null && !inserted) {
int compareLast = node.getLastName().compareTo(curr.getLastName());
//returns
//< 0 - node goes before curr
//== 0 - check first names
//> 0 - node goes after curr
if (compareLast < 0) { //if the first line is lexigraphically less than (comes before)
if (curr == head) {
head = node;
} else {
prev.setNext(node);
}
node.getCourse().insert(newCourse);
node.setNext(curr);
inserted = true;
} else if (compareLast == 0) { //last names are the same, check first names
int compareFirst = node.getFirstName().compareTo(curr.getFirstName());
if (compareFirst < 0) {
if (curr == head) {
head = node;
} else {
prev.setNext(node);
}
node.getCourse().insert(newCourse);
node.setNext(curr);
inserted = true;
} else if (compareFirst == 0) { //same student
//System.out.println("Error, they exist already.");
if (curr == head) {
head = node;
} else {
prev.setNext(node);
}
curr.getCourse().insert(newCourse); //insert into list for current node
inserted = true;
} else {
prev = curr; //advance previous to current
curr = curr.getNext(); //advance current to next field
}
} else { //the first line needs to be placed somewhere after the item compared to, continue
prev = curr;
curr = curr.getNext();
}//end else
}//end while
//Checks very last node
if (!inserted) {
int compareLast = node.getLastName().compareTo(curr.getLastName());
//returns
//< 0 - node goes before curr
//== 0 - check first names
//> 0 - node goes after curr
if (compareLast < 0) {
if (curr == head) {
head = node;
} else {
curr.setNext(node);
}
node.getCourse().insert(newCourse);
node.setNext(curr);
} else if (compareLast == 0) { //if last names are the same
int compareFirst = node.getFirstName().compareTo(curr.getFirstName());
if (compareFirst < 0) { //first name is lexigraphically less than second name
node.getCourse().insert(newCourse);
node.setNext(curr);
} else if (compareFirst == 0) {
System.out.println("Error, same person.");
} else { //the desired node goes after the current last node (on basis of first name)
curr.setNext(node);
}
} else { //the desired node goes after the current last node (on basis of last name)
curr.setNext(node);
}
}//end if
}//end else
}//end insert