• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointerException in Treeset

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello guyz,

set interface is not threadsafe & if i am implementing Hashset than it allows me to have null value as a property.but when it come to TreeSet implementation,it does not allow me to specify null as a property,when i run the code it will give me NullPointerException.does anyone know the reason?
need your help
regards..
NppJavaGeek90
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nisu pathak wrote:
set interface is not threadsafe & if i am implementing Hashset than it allows me to have null value as a property.but when it come to TreeSet implementation,it does not allow me to specify null as a property,when i run the code it will give me NullPointerException.does anyone know the reason?




The common version of TreeSet (that does natural ordering) takes Comparable elements. And although null IS-A Comparable, it can't be used as one -- it will generate an NPE when trying to call the compareTo() method.

If you want to place null values into the TreeSet, you will need to provide a Comparator to order to sort them.

Henry
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you create a TreeSet without a Comparator, it calls the compareTo method on its elements. That method is almost never null-safe. The solution: create a custom Comparator that handles nulls and use that in the TreeSet creator:
With Java 8 it's even easier:
 
Greenhorn
Posts: 2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TreeSet is implemented as a TreeMap internally which takes in the element which is being added to a TreeSet as the key of the map and value would be a plain java object using new Object(). As per the specs, TreeMap doesn't allow key to be stored as Nulls hence, when you try to add a null element into a TreeSet a NPE is thrown.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

may naren wrote:TreeSet is implemented as a TreeMap internally which takes in the element which is being added to a TreeSet as the key of the map and value would be a plain java object using new Object(). As per the specs, TreeMap doesn't allow key to be stored as Nulls hence, when you try to add a null element into a TreeSet a NPE is thrown.




As Rob and I already mentioned, this isn't always true. If you want the TreeMap to handle null keys, you need to provide a Comparator that supports comparing with nulls.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic