Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Adding Student Objects into singly linked list alphabetically [NullPointerException]

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding Student Objects into singly linked list alphabetically [NullPointerException]

    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());
    }
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Adding Student Objects into singly linked list alphabetically [NullPointerException]

    getting a NullPointerException at lines 148 ,
    There is a variable with a null value on line 148. Look at line 148 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 148 and print out the values of all the variables on that line.

    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
    What have you tried? What logic will the code need to check last names and if equal, first names?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Adding Student Objects into singly linked list alphabetically [NullPointerException]

    If you are looking for a list that will sort using the compareTo method, i would suggest a TreeSet. Check out the javadocs for that. It is pretty impressive.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. Singly Linked List
    By koala711 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2012, 02:15 PM
  2. Singly-Linked list structure
    By blaster in forum Algorithms & Recursion
    Replies: 24
    Last Post: March 11th, 2012, 03:42 PM
  3. [SOLVED] Linked List (adding alphabetically)
    By Usoda in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:17 PM
  4. Singly Circular Linked List Error
    By clydefrog in forum Collections and Generics
    Replies: 7
    Last Post: March 5th, 2012, 08:17 PM
  5. Singly Linked List of Integers, get(int i) function throws Null Pointer Exception
    By felixtum2010 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 06:55 PM

Tags for this Thread