• 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

TreeSet Problem

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


Output of this prograe is nullpointer exception

Can anybody tell me why it is showing nullpointer exception.

While Code



is producing output: 1

Please tell me if any body is having idea abt it....

Thanks
-Sharad Garg
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How will a TreeSet compare two null values?
That might give a clue to where the NullPointer is coming from (a further clue will be in the exception stack, i.e. the line that caused the whole problem).
 
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

Hint: if you use a comparator, then it will work too.

Henry
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Garg

It gives Null pointer exception in the first program since you are adding 2 null values to the TreeSet. By default the TreeSet sorts data according to the natural order or sorting mechanism which you have provided in the comparator. Since you have added 2 null values the compiler doesn't know how to sort 2 null values where as if it is a single null value it doesn't need to sort the data hence it doesn't give any runtime error.

Note even if you insert one null value and another string it gives runtime error. That is the reason you get error for the first program and no error in the second one.

I hope I cleared your question.

Thanks
Vijay
 
Garg Sharad
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your clarification

-Sharad
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic