aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Basic question, this is my first time using Comparable and compareTo... Why does not work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Basic question, this is my first time using Comparable and compareTo... Why does not work? " Watch "Basic question, this is my first time using Comparable and compareTo... Why does not work? " New topic
Author

Basic question, this is my first time using Comparable and compareTo... Why does not work?

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Basic question, this is my first time using Comparable and compareTo... Why does not work? Runtime Expection... NullPointerException...

Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Because in the compareTo(ContaPoupanca contaPoupanca) method you are using contaPoupanca without checking to see that it is not null. In main(), you have new ContaCorrente().compareTo(null);

Also I noticed that in compareTo(ContaPoupanca contaPoupanca), you are returning -1 or 0. The compareTo() method should return three values; a negative number, 0, and a positive number.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Also, when implementing Comparable, you should compare two instances of the same class.
should be:
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
if is possible, you could type the code for me I would love that... because I try to learn it by myself and that's really difficult... hehehe you all have been teaching me a lot...
bye,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic question, this is my first time using Comparable and compareTo... Why does not work?