import java.util.TreeSet;
class Test
{
public static void main(String[] args)
{
TreeSet t=new TreeSet();
t.add(null);
System.out.println(t);
}
}
output: NullPointerException. I read in many articles that empty TreeSet will accept null for first time but am getting NullPointerException...am using java7..can any body clarify my doubt....
The documentation for TreeSet#add in Java 7 states:
NullPointerException - if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements
So since you have not specified a custom comparator implementation that can handle null values, you get the NPE.
Edit: It was possible to add a null
element as the first element of a TreeSet
/TreeMap
in Java 6, but it was considered a bug:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With