user3516780
user3516780

Reputation: 149

Adding null to empty TreeSet raising NullPointerException

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....

Upvotes: 13

Views: 20564

Answers (7)

Ankit Tripathi
Ankit Tripathi

Reputation: 1

In Java from Version 1.7 onwards, it is not allowed the first element as null on For Tree set it will give Runtime exception -NullPointerException. Same if you can try in java 1.6 it will get compiled and run also when adding null on the first element but after that add any element it will give null point exception.

Upvotes: 0

Ashwini Raut
Ashwini Raut

Reputation: 1

For Empty TreeSet as 1st element null Insertion is possible. But after inserting that null if we are trying to insert any element we will get NullPointerException. And for Empty TreeSet if we are trying to Insert null we will get NullPointerException

Upvotes: -1

Anil Nivargi
Anil Nivargi

Reputation: 1717

TreeSet internally uses Comparable interface to sort the elements in ascending order. compareTo() method of Comparable interface compare two values each other to find the greater value. When if you add null as element, can not compare null with other value to decide which one is the larger/greater value, for this reason compareTo() method throws NullPointerException.

Declaration of add method in TreeSet is,

public boolean add(E e) throws ClassCastException, NullPointerException;

Upvotes: 0

Sathya
Sathya

Reputation: 31

From 1.7 onwards null is not at all accepted by TreeSet. If you enforce to add then we will get NullPointerException. Till 1.6 null was accepted only as the first element.

Upvotes: 3

Maddy
Maddy

Reputation: 21

API explanation:

Synopsis: Inserting an Invalid Element into a TreeMap Throws an NPE Description: Due to an error in java.util.TreeMap, it was previously possible to insert invalid null elements and elements not implementing the Comparable interface into an empty TreeMap or TreeSet. Only a single invalid element could be inserted into an empty TreeMap or TreeSet; additional elements would cause the expected NullPointerException or ClassCastException. Most other operations upon the collection would also fail. As of Java SE 7, inserting an invalid null element or an element not implementing Comparable into an empty TreeMap or TreeSet throws a NullPointerException.

Upvotes: 2

user7471158
user7471158

Reputation: 51

This is because for an empty TreeSet at the first element a null value can be inserted, but after inserting that first value if we are trying to insert any other objects then we will get a NullPointerException.

For a non-empty TreeSet, if we are trying to insert a null value at run time you will get a NullPointerException. This is because when some elements exist in the tree, before inserting any object it compares the new object to the existing ones using the compareTo() method and decides where to put the new object. So by inserting null the compareTo() method internally throws NullPointerException.

I think in the new version of Java the null insertion is not allowed.

Upvotes: 5

NilsH
NilsH

Reputation: 13821

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:

Upvotes: 25

Related Questions