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