Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basic question, this is my first time using Comparable and compareTo... Why does not work? Runtime Expection... NullPointerException...

 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, when implementing Comparable, you should compare two instances of the same class.
should be:
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic