scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

TreeSet contains method null point exception #9024

Closed scabug closed 9 years ago

scabug commented 9 years ago

Run the following under 2.11.0. It throws null point exception as well for 2.10.4 and 2.9.3 although the implementation has been changed.

Welcome to Scala version 2.11.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_60).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val set2 = new scala.collection.immutable.TreeSet[String]()
set2: scala.collection.immutable.TreeSet[String] = TreeSet()

scala> set2 + "A" + "B"
res0: scala.collection.immutable.TreeSet[String] = TreeSet(A, B)

scala> res0.contains(null)
java.lang.NullPointerException
  at scala.math.Ordering$StringOrdering$class.compare(Ordering.scala:327)
  at scala.math.Ordering$String$.compare(Ordering.scala:329)
  at scala.math.Ordering$String$.compare(Ordering.scala:329)
  at scala.collection.immutable.RedBlackTree$.lookup(RedBlackTree.scala:40)
  at scala.collection.immutable.RedBlackTree$.contains(RedBlackTree.scala:32)
  at scala.collection.immutable.TreeSet.contains(TreeSet.scala:144)
  ... 40 elided
scabug commented 9 years ago

Imported From: https://issues.scala-lang.org/browse/SI-9024?orig=1 Reporter: Jiezhen Yi (Jiezhen)

scabug commented 9 years ago

@kanielc said (edited on Dec 5, 2014 6:06:42 AM UTC): Judging by the spec of the Java TreeSet, this seems consistent. With natural ordering, asking for null should throw a NPE. You can provide a custom null aware Ordering.

val set2 = new scala.collection.immutable.TreeSetString

scabug commented 9 years ago

Jiezhen Yi (Jiezhen) said: Mark as won't fix.