Using compareTo() in a Linked List Please Help!!

I'm trying to compare strings in a Linked List and it throws a null pointer exception somewhere in the method smallest() which is where i have the strings(nodes) comparing each other. I've been Trying to find the answer for two days now and I've gotten nowhere. PLEASE HELP! The code is as follows:
public String smallest()
  // Returns smallest String in StringLog in terms of lexicographic ordering.
   //Precondition: StringLog is not empty.
LLStringNode node;
LLStringNode node2;
LLStringNode node3 = log;
node = log;
node2 = log;
String smallString = "Bob";
boolean notNull = (node != null);
boolean notNull2 = (node2 != null);
while (notNull && notNull2)
     System.out.println(node.getInfo() + " " + node2.getInfo());
     if (node.getInfo().compareTo(node2.getInfo()) <= 0)
       node3 = node;
       node2 = node2.getLink();
       smallString = node3.getInfo();
     else if (notNull && notNull2)
       node3 = node2;
      node =  node.getLink();
      smallString = node3.getInfo();
      smallString = node3.getInfo();
      return smallString;
}I've inserted a line of code that shows the output of the method in the test driver before it throws the exception it is as follows: note I have already input strings through the test driver.
Choose an operation:
1: insert(String element)
2: howMany(String element)
3: clear()
4: contains(String element)
5; isFull()
6; isEmpty()
7: size()
8: uniqInsert(String element)
9: getName()
10: toString()
11: smallest()
12: Stop testing
11
Exception in thread "main" lamb lamb
lamb cat
cat cat
cat apple
apple apple
apple dog
apple thing
apple girl
apple boy
java.lang.NullPointerException
     at LinkedStringLog.smallest(LinkedStringLog.java:189)
     at CMPS39001.main(CMPS39001.java:121)Below is the full class of LinkedStringLog which contains the smallest() method(you may or may not need this i'm not sure)
{codepublic class LinkedStringLog implements StringLogInterface
  protected LLStringNode log; // reference to first node of linked
                              // list that holds the StringLog strings
  protected String name;      // name of this StringLog
  public LinkedStringLog(String name)
  // Instantiates and returns a reference to an empty StringLog object
  // with name "name".
    log = null;
    this.name = name;
public void insert(String element)
// Precondition: This StringLog is not full.
// Places element into this StringLog.
LLStringNode newNode = new LLStringNode(element);
newNode.setLink(log);
log = newNode;
public int howMany(String element)
// Returns an int value of how many times it occurs in StringLog
int eleCount = 0;
     LLStringNode node;
     node = log;
     while (node != null)
     if (element.equalsIgnoreCase(node.getInfo()))
          eleCount ++;
          node = node.getLink();
          else
          node = node.getLink();
     return eleCount;
public boolean isFull()
// Returns true if this StringLog is full, false otherwise.
return false;
public boolean isEmpty()
// Returns true if StringLog is empty, it otherwise returns false.
     boolean isNull = true;
     LLStringNode node;
     node = log;
     boolean searchEmpty = (node != null);
if (searchEmpty)
     isNull = false;
     return isNull;
return isNull;
public boolean uniqInsert(String element)
// Inserts element in stringLog unless an identical string already exists in the StringLog
LLStringNode node;
     node = log;
     boolean found = false;
     boolean searchMore;
     searchMore = (node != null);
     while (searchMore && !found)
     if (element.equalsIgnoreCase(node.getInfo()))
          return found;
          else
          node = node.getLink();
          searchMore = (node != null);
     if (found = true)
          LLStringNode newNode = new LLStringNode(element);
          newNode.setLink(log);
          log = newNode;
     return found;
public int size()
// Returns the number of Strings in this StringLog.
int count = 0;
LLStringNode node;
node = log;
while (node != null)
count++;
node = node.getLink();
return count;
public boolean contains(String element)
// Returns true if element is in this StringLog,
// otherwise returns false.
// Ignores case difference when doing string comparison.
LLStringNode node;
node = log;
boolean found = false;
boolean moreToSearch;
moreToSearch = (node != null);
while (moreToSearch && !found)
if (element.equalsIgnoreCase(node.getInfo())) // if they match
found = true;
else
node = node.getLink();
moreToSearch = (node != null);
return found;
public void clear()
// Makes this StringLog empty.
log = null;
public String getName()
// Returns the name of this StringLog.
return name;
public String toString()
// Returns a nicely formatted string representing this StringLog.
String logString = "Log: " + name + "\n\n";
LLStringNode node;
node = log;
int count = 0;
while (node != null)
count++;
logString = logString + count + ". " + node.getInfo() + "\n";
node = node.getLink();
return logString;
public String smallest()
// Returns smallest String in StringLog in terms of lexicographic ordering.
//Precondition: StringLog is not empty.
LLStringNode node;
LLStringNode node2;
LLStringNode node3 = log;
node = log;
node2 = log;
String smallString = "Bob";
boolean notNull = (node != null);
boolean notNull2 = (node2 != null);
while (notNull && notNull2)
     System.out.println(node.getInfo() + " " + node2.getInfo());
     if (node.getInfo().compareTo(node2.getInfo()) <= 0)
     node3 = node;
     node2 = node2.getLink();
     smallString = node3.getInfo();
     else if (notNull && notNull2)
     node3 = node2;
node = node.getLink();
smallString = node3.getInfo();
     smallString = node3.getInfo();
return smallString;

line 189 is
if (node.getInfo().compareTo(node2.getInfo()) <= 0)and the other line mentioned is just the call.
But that line runs several times until a certain point and then throws the error. Example, in the test driver every time that line is ran it is outputting the nodes in the test driver that I posted.

Similar Messages

Maybe you are looking for

  • It is possible to create something like this in AE?

    Hi, i want to know if its possible to create something like this in AE (not in 3D) but particles that 'builds up' the text in some way? i have a text that is a vector, and the same as PNG, it is possible? Please tell me!

  • Problem with asynchronous replication

    Hi All , In asynchronous replication, I am getting huge number of records in defcall view. its very difficult to take care of each row manually. Is there any way to create the INSERT/UPDATE/DELETE script from defcall or with the help of other views b

  • Materialized view refresh using connected user db links

    I have a general question. I have a materialized view that more or less joins tables from 2 databases. For example I have a table X in database A, and a table Y in database B. I create the materialized view in the schema owning table X in database A.

  • Regarding mapping

    Hi Buddies,    How do u transport mapping from development to quality server. How many types of mapping r there. Thanks in advance Regards, Nagaraju Tankala

  • Credit Management: Any setting to warn or avoid Project creation in PS

    Is there any standard credit management setting that would warn or doesn't allow to save the creation of the project in PS , just like warning or stop from saving the service notification by credit management