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 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;
}
}
Also, When I ran the test driver I had the system print lines onto the console until it broke and it looked like this. *note* The Strings have already been input into the test driver at this point.
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" java.lang.NullPointerException
at LinkedStringLog.smallest(LinkedStringLog.java:189)
at CMPS39001.main(CMPS39001.java:121)
girl girl
girl apple
apple apple
apple ass
apple boy
apple purple
Thank you all, Your help is most appreciated.