This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes TreeSet problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "TreeSet problem" Watch "TreeSet problem" New topic
Author

TreeSet problem

Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
Posts: 89


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.


"Control time instead of letting time control you."
Gilles Marceau
Ranch Hand

Joined: Feb 17, 2007
Posts: 78
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.


SCJP 1.5<br />SCJD 1.6<br />SCBCD in progress...
Gilles Marceau
Ranch Hand

Joined: Feb 17, 2007
Posts: 78
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

Joined: Nov 14, 2005
Posts: 324
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.


"Know where to find the solution and how to use it - that's the secret of success."
Nadeem Khan
Ranch Hand

Joined: Nov 27, 2007
Posts: 108
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!!


<i>If there were no Exceptions (not Errors!), Life would have kinda sucked!!</i>
Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
Posts: 89
Thanks Roadie Engineer your explanation may be right.
[ March 03, 2008: Message edited by: Prasun Howlader ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

"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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TreeSet problem
 
Similar Threads
TreeSet of enum not sorted
Collection's Problem
TreeSet Problem
About combo boxes and refressing data UrlyBird
preparing for scjp 5.0