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
-
I want to buy an in-app purchase but i don`t remember my security questions and i cant access my recovery email either, what can i do? i have 100$ on my account and cant use it because of that problem, please help URGENT
If you have a rescue email address on your account then you can use that - follow steps 1 to 5 half-way down this page will give you a reset link on your account : http://support.apple.com/kb/HT5312
If you don't have a rescue email address (you won't be able to add one until you can answer your questions) then you will need to contact Support in your country to get the questions reset : http://support.apple.com/kb/HT5699 -
i have problem with my wifi in 4 S, i cant connect to any wifi itried resetting network setting and reset all setting but the result was the same, its only keeps searching for wifi and cant find any, itried to use OTHER but also didnt work.please help me???
If Join was on then your home wi-fi must be set to Non-Broadcast. If you did not set this up (maybe your provider did) then you will need to find the Network Name they used, and any password they used. The SSID is Security Set ID and to see more try http://en.wikipedia.org/wiki/SSID . Basically it is the name used to identify your router/network. A lot of times the installer will leave it set as LinkSys, or Broadcom or whatever the manufacturer set it as for default. Your best bet is to get whoever installed it to walk you through how they set it up, giving you id's and passwords so you can get in. HOWEVER, if you are not comfortable with this (if you set security wrong, etc.) you would be well ahead of the game to hire a local computer tech (networking) to get this working for you. You can also contact the vendor of your router and get help (if it is still in warranty), or at least get copies of the manuals as pdf files. Sorry I can't give you more help, I hope this gives you an idea where to go from here to find more.
-
After installing iPhone Configutilities in my Mac, Personal Hotspot using USB is not working! Please help, I am using 10.7 Lion OS
Did you talk to your carrier?
-
I am on Vacations in Nicaragua and I just lost my Ipod. I did not turned on the ICloud. How can I use "Find my Ipod".? please help me.
You cannot use it. You would have had to set it up on your ipod itself before you lost it. Since you did not "turn on the icloud", then you have not set up find my ipod.
Sorry.
There is no way to track your ipod. -
Dear Apple,
I have an iPhone 4 32GB (black), i bought from softbank, japan. Now, I have recently finished a graduate and i'll comeback home in Laos. But i can't continue to use my iphone.
Could you please help me to unlock my phone so I can use it with other carrier Sims. I would appreciate it.
Please let me know if you could help me out. Thanks again.
<Edited by Host>Like ckuan said, Apple does not unlock iPhone; your carrier does, provided that the carrier offers this service.
Unfortunately, SoftBank does not offer authorized unlocking of iPhone.
Your best option is to sell this phone and buy another iPhone from the carrier of your choice or better buy a factory unlocked iPhone so you would be able to use it with any compatible GSM carrier around the world. -
Photoshop CS6 crashes all the time and especially when using the type tool... please help!
Since I did not have this problem with CS5 or CS4, I can only assume something is not right with Photoshop CS6... below is my video card info:
ATI Radeon HD 4850:
Chipset Model: ATI Radeon HD 4850
Type: GPU
Bus: PCIe
PCIe Lane Width: x16
VRAM (Total): 512 MB
Vendor: ATI (0x1002)
Device ID: 0x944a
Revision ID: 0x0000
ROM Revision: 113-B9110C-425
EFI Driver Version: 01.00.383
Displays:
iMac:
Resolution: 2560 x 1440
Pixel Depth: 32-Bit Color (ARGB8888)
Main Display: Yes
Mirror: Off
Online: Yes
Built-In: Yes
Connection Type: DisplayPort
Cinema HD:
Resolution: 1920 x 1200
Pixel Depth: 32-Bit Color (ARGB8888)
Display Serial Number: 2A80273WXMN
Mirror: Off
Online: Yes
Rotation: Supported -
My isight built in camera is only turning on for few second on my MacBook Pro (13-inch, Late 2011 with osx Yosemite) when I use any camera needed app. Please help me. Its getting a little annoying.
If restarting Mac or reinstalling OS X does not correct this symptom, you likely have a hardware problem. Contact Apple Support or an Apple Authorized Service Provider for service.
Message was edited by: EZ Jim
Mac OSX 10.10.1 -
Do I have to unlock iPad 2 which I bought from Uk and I want to use it in Sri Lanka. Please help.
Depends where you purchased it. If it was directly from a cell provider at a subsidized price, it may be locked. Orange in the UK does this.
-
my iPad shut down on its own and when i switched it on, it asks to 'Activate iPad' and reads below 'This iPad was lost and erased. Sign in with the Apple ID that was used to erase this iPad". Please help!!!!
Is it possible that you or someone who knows your iCloud sign on went into the Find My iPhone app and erased your device via iCloud? Because that is what that message is saying.
Have you tried to sign on with your Apple ID and password?
Was it online prior to this incident happening?
GB -
I am a user of iPhone 4 that they gave to me .. then suddenly my brother restored it and we cannot identify the apple id and password that have used to unlock this phone.. please help me .. because i can't use anymore my phone
Please Help me .. im from Philippines
thx a lot apple company
<Email Edited By Host>This is not Apple we are all users here
You will need to obtain the original Apple id ,that is the only way to unlock it
Speak to the original owner .If you cannot it is likely the iPhone was stolen and the activation lock is preventing use by a third party that is you -
what should i do. when i start my phone the message display me like the sim card inserted in this phone does not appear to be supported. only the compitable sim appear to be supported carrier may be used to active the iphone,
now please help me with probelm i am having an iphone5 with me but what should i do with it bcs it dosent work my carrier please help me what should i do ?Send it back, as you won't be able to activate a Sprint iPhone in India, nor will you be able to get the sim slot unlocked since you're not a Sprint customer. Your parents bought the wrong iPhone.
-
My wife got a Iphone in Bangladesh. she do not know the password for apple ID, there is no way to collect it, as the email id for that iphone is not her. how she can use application store and itunes? please help me
If it is second hand, try putting it in recovery mode, and restoring it with itunes on a mac or pc.
Recovery mode tutoral:http://www.youtube.com/watch?v=dkVr5CxFiFM -
Hey hi i want to buy apple iphone 16gb At&t But i want use in india how to Buy Please help me
hey hi i want to buy apple iphone 16gb At&t But i want use in india how to Buy Please help me
AT&T "low cost" options are for ATT subsidized phones. Those require a two year contract commitment and are only available in the USA and those phones are locked to ATT. You can get them unlocked only once you meet the contract commitments. Such a phone would not be available for sale to you (as the contract requires USA payment and billing) and would be useless to you in India.
-
My iphoto 09 keeps on downloading duplicates to a temp folder without asking when I drag a photo from firefox over to iphoto to import it. It does not happen when using safari. can some one please help?
You're in the wrong forum. Post to iPhoto for iLife, not iPhoto for iOS.
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.
-
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