Hello everyone,
I'm trying to compare Strings in a Linked List that were input by user and return the smalles(lexicographically) to the console. Any help is appreciated.

** actual method

public String smallest()
  // Returns smallest String in StringLog in terms of lexicographic ordering.
   //Precondition: StringLog is not empty.
  {
 LLStringNode node;
 LLStringNode node2;
 LLStringNode node3 = null;
 node = log;
 node2 = log;
 
 while (node != null)
{			
	if (node.getInfo().compareTo(node2.getInfo()) < 0)
	{
	  node2.getLink();
	  node3 = node;
	}  
	else
	{
	  node = node2;
      node2.getLink();
	  node3 = node2;
		}
	 } 
     String smallString = node3.getInfo() ;
	 return smallString;
  }
}

** Call from Test Driver

case 11: // smallest                                                                 System.out.println("Result: " + log.smallest());
 break;

Recommended Answers

All 7 Replies

But what problem are you having? You just posted code with no apparent errors or explanation.

sorry, it doesnt error out. It just seems to break at

if (node.getInfo().compareTo(node2.getInfo()) < 0)

I've looked everywhere for correct syntax on how to compare strings in a linked list and ive gotten no luck.

correction it throws
Exception in thread "main" java.lang.NullPointerException
at LinkedStringLog.smallest(LinkedStringLog.java:187)
at CMPS39001.main(CMPS39001.java:121)

correction it throws
Exception in thread "main" java.lang.NullPointerException
at LinkedStringLog.smallest(LinkedStringLog.java:187)
at CMPS39001.main(CMPS39001.java:121)

At the line specified you are using something which is null.

these are the lines that it is pointing to

if (node.getinfo().compareTo(node2.getInfo() < 0)




case 11: // smallest                                                                 System.out.println("Result: " + log.smallest());

At the line specified you are using something which is null.

Like I said, at that line something is null. Not to mention that you messed up with the opening and closing of the parenthesis.

The error JavaAddict is referring to basically means that either node or node2 were not properly set (i.e. the Object was never created, for example), or that at some point, you explicitly set the reference to null. If you look at where you said node = log and node2 = log, you should trace the origin of 'log' because there is a good chance that log itself was not properly set.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.