Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeSet problem

 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When i add set.add(null) first in the treeSet then no runtime error but if i add set.add(null) last or middle then runtime exception occur. What is the reason can any one explain.
 
Gilles Marceau
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the javadoc for TreeSet.add method, it states that :

NullPointerException - if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements.

As you created the TreeSet with its default constructor, it uses
natural ordering.

So, adding null to the TreeSet is not supported.
 
Gilles Marceau
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more remark : if you are planning to make more test with the Comparable
interface, you should fulfill the compareTo requirements, or the collections
supporing sorting (such as TreeSet) won't work properly. For example, the implementation of Sub.compareTo method is not correct, because a.compareTo(b) is not equal to - b.compareTo(a).
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gilles Marceau:
So, adding null to the TreeSet is not supported.


Why it does not give error when null is added as first element.
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prasun,
did you get the answer?
"compareTo" is called for a set whenever you are inserting an element except at the first insertion when we dont need to know the position of insertion.

Now i think when you insert null in the middle or last , it calls null.compareTo(Object), hence NullPointerException!! But when you insert null at the beginning , compareTo isnt being called on null!!
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roadie Engineer your explanation may be right.
[ March 03, 2008: Message edited by: Prasun Howlader ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Roadie Engineer", you have previously been warned on multiple occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional, and this is your final warning. Adjust your display name to comply with the required standards prior to your next post.

Failure to comply will result in the removal of your account.

bear
JavaRanch Sheriff
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic