having some problems with my add function it is supposed to add another node to the list in a sorted order but i don't think its working at all and thats why im getting a null pointer exception in my print function. thank you in advance!
SortedListDriver.java
/** * @author lisit * */ public class SortedListDriver { /** * @param args */ public static void main(String[] args) { SortedLList slist = new SortedLList(); slist.add(1); slist.print(); slist.add(3); slist.print(); slist.add(2); slist.print(); slist.add(5); slist.print(); slist.add(4); slist.print(); } }
SortedLList.java
/** * @author lisit * * @param <T> */ public class SortedLList<T extends Comparable<T>> implements SortedListInterface<T> { private Node firstNode; private int numberOfEntries; public SortedLList() { Node n = new Node(); } @Override public void add(T newEntry) { Node<T> newNode = new Node(newEntry); if(isEmpty()) firstNode = newNode; else { Node<T> compareNode = firstNode; for(int i=0; i<numberOfEntries-1; i++){ T compareData = compareNode.getData(); if(newEntry.compareTo(compareData) > 0){ System.out.println("IF STATEMENT WORKED"); Node nodeBefore = getNodeAt(i-1); Node nodeAfter = nodeBefore.getNextNode(); newNode.setNextNode(nodeAfter); nodeBefore.setNextNode(newNode); } else{ compareNode.getNextNode(); System.out.println("compareNode " + compareNode.getData()); } } } numberOfEntries++; } @Override public boolean remove(T anEntry) { // TODO Auto-generated method stub return false; } @Override public int getPosition(T anEntry) { // TODO Auto-generated method stub return 0; } @Override public T getEntry(int givenPosition) { // TODO Auto-generated method stub return null; } @Override public boolean contains(T anEntry) { // TODO Auto-generated method stub return false; } @Override public int getLength() { return numberOfEntries; } @Override public boolean isEmpty() { boolean result; if(numberOfEntries == 0){ assert firstNode == null; result = true; } else{ assert firstNode != null; result = false; } return result; } @Override public T[] toArray() { // TODO Auto-generated method stub return null; } @Override public T remove(int givenPosition) { // TODO Auto-generated method stub return null; } @Override public void clear() { firstNode = null; numberOfEntries = 0; } public void print(){ Node listNode = firstNode; System.out.println("Number of Entries: " + numberOfEntries); for(int i=0; i<numberOfEntries; i++){ System.out.println(listNode.getData()); listNode = listNode.getNextNode(); } } private Node getNodeAt(int givenPosition) { assert !isEmpty() && (1 <= givenPosition) && (givenPosition <= numberOfEntries); Node currentNode = firstNode; for (int counter = 1; counter < givenPosition; counter++) currentNode = currentNode.getNextNode(); assert currentNode != null; return currentNode; } }
Node.java
public class Node<T> { private T data; private Node next; public Node(){ data = (T) null; } public Node(T dataPortion) { this(dataPortion, null); } public Node(T dataPortion, Node nextNode) { data = dataPortion; next = nextNode; } public T getData() { return data; } public Node getNextNode() { return next; } public void setNextNode(Node nextNode) { next = nextNode; } }
output
Number of Entries: 1 1 Number of Entries: 2 1 Exception in thread "main" java.lang.NullPointerException at SortedLList.print(SortedLList.java:113) at SortedListDriver.main(SortedListDriver.java:17)
EDIT:
SortedListInterface.java
/** * An interface for the ADT sorted list. Entries in the list have positions that * begin with 1. * * @param <T> * The type of object stored in the sorted list */ public interface SortedListInterface<T extends Comparable<T>> { /** * Adds a new entry to this sorted list in its proper order. * * @param newEntry * the object to be added as a new entry */ public void add(T newEntry); /** * Removes a specified entry from this sorted list. * * @param anEntry * the object to be removed * @return true if anEntry was located and removed */ public boolean remove(T anEntry); /** * Gets the position of an entry in this sorted list. * * @param anEntry * the object to be found * @return the position of the first or only occurrence of anEntry if it * occurs in the list; otherwise returns the position where anEntry * would occur in the list, but as a negative integer */ public int getPosition(T anEntry); /** * Retrieves the entry at a given position in this list. * * @param givenPosition * an integer that indicates the position of the desired entry * @return a reference to the indicated entry or null, if either the list is * empty, givenPosition < 0, or givenPosition > getLength() - 1 */ public T getEntry(int givenPosition); /** * Sees whether this list contains a given entry. * * @param anEntry * the object that is the desired entry * @return true if the list contains anEntry, or false if not */ public boolean contains(T anEntry); /** * Gets the length of this list. * * @return the integer number of entries currently in the list */ public int getLength(); /** * Sees whether this list is empty. * * @return true if the list is empty, or false if not */ public boolean isEmpty(); /** * Retrieves all entries that are in this list in the order in which they * occur in the list. * * @return an array of the entries of this list */ public T[] toArray(); /** * Removes the entry at a given position from this list. Entries originally * at positions higher than the given position are at the next lower * position within the list, and the list's size is decreased by 1. * * @param givenPosition * an integer that indicates the position of the entry to be * removed * @return a reference to the removed entry or null, if either the list was * empty, givenPosition < 0, or givenPosition > getLength() - 1 */ public T remove(int givenPosition); /** Removes all entries from this list. */ public void clear(); } // end SortedListInterface