*
The moose likes Java in General and the fly likes need help with compareTo() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "need help with compareTo()" Watch "need help with compareTo()" New topic
Author

need help with compareTo()

Doug Wolfinger
Greenhorn

Joined: Jul 27, 2002
Posts: 18
I am trying to add String items to a sorted array list, using compareTo to find the proper position for each item.
This is the runtime error I get when I run ListPop.java with sortedAdd() defined with a String, instead of Object, in the argument list.
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.compareTo(String.java:789)
at java.lang.String.compareTo(String.java:825)
at SortedList.sortedAdd(SortedList.java:41)
at ListPop.main(ListPop.java:17)
When I try to define sortedAdd() with an Object, instead of a String, in the argument list I get this error message when I compile SortedList.java
SortedList.java:41: cannot resolve symbol
symbol : method compareTo (java.lang.Object)
location: class java.lang.Object
while(item.compareTo((Object)arr[pos]) <= 0) {
1 error
Here is SortedList.java

Here is FullName.java, which implements compareTo()

Here is ListPop.java, which attempts to populate the array.

It should put the first item in the array here

so why is it giving me the NullPointerException for arr[]?
New ones should be added when the while loop in sortedAdd() finishes, right?
Thanks!
[ October 09, 2002: Message edited by: Doug Wolfinger ]
[ October 09, 2002: Message edited by: Doug Wolfinger ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Regarding your compilation problem: Object doesn't define compareTo - you should use Comparable instead.
Regarding the NullPointerException: what does happen if you reach the end of the list in the loop?
BTW, I have the strong feeling that what you try to do would be much easier to accomplish by using an ArrayList instead of an array, in combination with Collections.binarySearch(...)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Try this:
while(item.compareTo((String)arr[pos - 1]) <= 0)
You are saying that pos = length. If your length is let's say 3, then you are trying to get an item from the array at position 3 which does not exist. The position at the end of the array would be 2 instead of 3. Hope this helps!
Barry
Doug Wolfinger
Greenhorn

Joined: Jul 27, 2002
Posts: 18
Regarding the end of the loop, I suppose I could add
However, I'm now getting an "Incompatible types" compile error from the while statement in sortedAdd(). The compiler says "found: int" "required: boolean", and points to the paren that follows "compareTo". So I've got my work cut out for me.
Btw, how do I capture the console's output. I thought "javac pgmname.java > pgmname.txt" would places the output in the text file, but it's not working. Thanks!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I agree with Ilja - it seems like it would probably be easier to use an ArrayList and methods like Collections.sort() and Collections.binarySearch(). Or use a TreeSet which may be even simpler. But anyway...
However, I'm now getting an "Incompatible types" compile error from the while statement in sortedAdd(). The compiler says "found: int" "required: boolean", and points to the paren that follows "compareTo". So I've got my work cut out for me.
Sounds like you need to look carefully at all the parentheses in the while statement. If this continues to trouble you, show us exactly what that while statement currently looks like.
Btw, how do I capture the console's output. I thought "javac pgmname.java > pgmname.txt" would places the output in the text file, but it's not working. Thanks!
That command only redirects standard output - not error messages. Try
javac pgmname.java > pgmname.txt 2>&1
The 2 refers to the error stream, while 1 refers to the standard output. 2>&1 means "send errors to the same place standard output is going."


"I'm not back." - Bill Harding, Twister
Doug Wolfinger
Greenhorn

Joined: Jul 27, 2002
Posts: 18
Hi,
I am still trying to figure out this method compareTo.
Node2.java implements Comparable. So, in using Node2 to cast in the comparison, I would think that I'm fulfilling the obligation to use a Comparable object to do that. Everything I am comparing is an Object, so I can't see what the problem is. The 3 compiler errors are "can't resolve symbol."
Thanks!
Here is Node2, which contains the compareTo method, making the Node2 type Comparable.

Here is Noding6, which extends Node2 and implements compareTo.

[ October 18, 2002: Message edited by: Doug Wolfinger ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: need help with compareTo()
 
Similar Threads
Find min and max in ArrayList
Unsorted array list
Unable to add object to array.
Retrieving the first item in list
next() cannot override next() in my abstract list.