Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

need help with compareTo()

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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(...)
 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
Doug Wolfinger
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic