With this code I am getting a NullPointerException at lines 148 , 184, and 198
Also I am also not even sure I am doing the coding right for checking nodes and adding them into a linked list correctly. I will not be using Collections sort or Comparable.
Would appreciate any help, and Yes I know exactly why Im getting the NullPointerException my actual question is how would I go about getting the addToList method to use the compareTo method to add the names in Alphabetical order by last name, and if they are equal, then by first name
Here is my current code:
import java.util.*; import java.io.*; /** * @author * linkedListProject class, reads from file a provided classRoster then sorts them alphebetically by last name into the linkedList * */ public class linkedListProject { private String firstName; private String lastName; private static LinkedList<linkedListProject> classRoster = new LinkedList<linkedListProject>(); private static class Node { linkedListProject data; private Node next = null; private Node(linkedListProject data, Node next) { this.data = data; this.next = next; } private Node(linkedListProject data) { this.data = data; } public linkedListProject getData() { return data; } } private static Node head; /** * Default Constructor */ public linkedListProject() { head = null; } /** * Constructor * @param firstName First name of student * @param lastName Last Name of Student */ public linkedListProject(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public void addToStart(linkedListProject data) { head = new Node(data, head); } /** * Getter for firstName * @return firstName */ public String getFirstName() { return firstName; } /** * Setter for firstName * @param firstName */ public void setFirstName(String firstName) { this.firstName = firstName; } /** * Getter for lastName * @return lastName */ public String getLastName() { return lastName; } /** * Setter for Last Name * @param lastName */ public void setLastName(String lastName) { this.lastName = lastName; } /** * toString method used for testing */ @Override public String toString() { return "linkedListProject [firstName=" + firstName + ", lastName=" + lastName + "]"; } /** * compareTo method used in sorting */ /** * Prints out list to console */ public void printList() { int count = 0; System.out.println("First Name: " + "Last Name: "); while (count < classRoster.size()) { linkedListProject a = classRoster.get(count); System.out.println(a.getFirstName() + " \t" + a.getLastName()); count++; } } /** * * @param a linked list ran in */ public static void addToList(linkedListProject a) { Node temp = head; Node nextNode = null; while (temp != null && nextNode.data.compareTo(a) >=0) { nextNode = temp; temp = temp.next; } if (temp == null) { Node cur2 = new Node(a); cur2.next = head; head = cur2; } else { Node cur2 = new Node(a); cur2.next = temp; if (nextNode == null) { nextNode.next = cur2; } classRoster.add(head.data); } } /** * Reads in students names into object then adds and sorts list based on last name & firstName * @param inputFileName The File Name * @throws java.io.IOException */ public void read(String inputFileName) throws java.io.IOException { Scanner infile = new Scanner(new FileReader(inputFileName)); while(infile.hasNext()) { String firstName = infile.next(); String lastName = infile.next(); linkedListProject intoList = new linkedListProject(firstName, lastName); linkedListProject.addToList(intoList); } infile.close(); } /** * Driver * @param args * @throws IOException */ public static void main(String[] args) throws IOException { linkedListProject test = new linkedListProject(); test.read("179ClassList.txt"); test.printList(); } public int compareTo(linkedListProject arg0) { if (this.lastName.equals(arg0.getLastName())) return this.firstName.compareTo(arg0.getFirstName()); else return this.lastName.compareTo(arg0.getLastName()); } }