Java 将 null 添加到空 TreeSet 引发 NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24489176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding null to empty TreeSet raising NullPointerException
提问by user3516780
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....
输出:空指针异常。我在很多文章中读到空的 TreeSet 将第一次接受 null 但我得到 NullPointerException ......我正在使用 java7 ......任何机构都可以澄清我的疑问......
回答by NilsH
The documentation for TreeSet#addin Java 7 states:
Java 7 中TreeSet#add的文档指出:
NullPointerException - if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements
NullPointerException - 如果指定元素为 null 并且此集合使用自然排序,或者其比较器不允许 null 元素
So since you have not specified a custom comparator implementation that can handle null values, you get the NPE.
因此,由于您尚未指定可以处理空值的自定义比较器实现,因此您将获得 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:
编辑:这是有可能的添加null
元素作为第一元素TreeSet
/ TreeMap
Java 6中,但它被认为是一个错误:
回答by user7471158
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
.
这是因为对于TreeSet
第一个元素的空值,可以插入一个空值,但是在插入第一个值之后,如果我们尝试插入任何其他对象,那么我们将得到一个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
.
对于非空TreeSet
,如果我们尝试在运行时插入空值,您将获得NullPointerException
. 这是因为当树中存在某些元素时,在插入任何对象之前,它会使用该compareTo()
方法将新对象与现有对象进行比较,并决定将新对象放在哪里。因此,通过插入 nullcompareTo()
方法在内部抛出 NullPointerException
.
I think in the new version of Java the null insertion is not allowed.
我认为在新版本的 Java 中不允许插入空值。
回答by Maddy
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.
概要:将无效元素插入 TreeMap 引发 NPE 描述:由于 java.util.TreeMap 中的错误,以前可以将无效的 null 元素和未实现 Comparable 接口的元素插入空的 TreeMap 或 TreeSet。只有一个无效元素可以插入到空的 TreeMap 或 TreeSet 中;额外的元素会导致预期的 NullPointerException 或 ClassCastException。集合上的大多数其他操作也会失败。从 Java SE 7 开始,将无效的 null 元素或未实现 Comparable 的元素插入空的 TreeMap 或 TreeSet 会引发 NullPointerException。
回答by Sathya
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.
从 1.7 开始,TreeSet 完全不接受 null。如果您强制添加,那么我们将收到 NullPointerException。直到 1.6 null 才被接受为第一个元素。
回答by Anil Nivargi
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.
TreeSet 内部使用 Comparable 接口按升序对元素进行排序。Comparable 接口的 compareTo() 方法比较两个值以找到较大的值。如果添加 null 作为元素,则无法将 null 与其他值进行比较来决定哪个值更大/更大,因此 compareTo() 方法会抛出 NullPointerException。
Declaration of add method in TreeSet is,
TreeSet 中 add 方法的声明是,
public boolean add(E e) throws ClassCastException, NullPointerException;
public boolean add(E e) throws ClassCastException, NullPointerException;
回答by Ashwini Raut
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
对于 Empty TreeSet 作为第一个元素 null 插入是可能的。但是在插入 null 之后,如果我们尝试插入任何元素,我们将得到 NullPointerException。对于 Empty TreeSet,如果我们尝试插入 null,我们将得到 NullPointerException
回答by Ankit Tripathi
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.
从 1.7 版开始的 Java 中,不允许在 For Tree 集上的第一个元素为 null,它将给出运行时异常 -NullPointerException。同样,如果您可以在 java 1.6 中尝试,它也会在第一个元素上添加 null 时被编译并运行,但在添加任何元素之后,它将给出空点异常。