Skip to content

Instantly share code, notes, and snippets.

@RomanHargrave
Created January 25, 2015 22:46
Show Gist options
  • Save RomanHargrave/72d5bd40688d441d92c1 to your computer and use it in GitHub Desktop.
Save RomanHargrave/72d5bd40688d441d92c1 to your computer and use it in GitHub Desktop.
JavaConversions API 'null' bug
/*
* This demonstrates a bug that occurs in the collections `JavaConversions` API.
* This is __not__ a bug with the type system.
*
* The conversions API does not consider that it could potentially try to convert a `null` value
* that has been cast as java collection to a scala collection. Being that conversion procedures
* potentially involve calling functions upon the underlying collection __in order__ to achieve conversion
* it is not possible for the caller to simply perform a check against the converted collection to see if it
* is null, as the VM will throw a NullPointerException when any access is attempted to the underlying
* java collection (which is really a null reference)
*
* This was noticed and tested with Scala 2.11.4
* */
import java.util.{List => JList}
import scala.collection.JavaConversions._
val badList: JList[String] /* Content type does not matter */ = null.asInstanceOf[JList[String]]
val badSeq: Seq[String] = badList // The compiler will wrap this with a converter. Invocation of the converter will lead to the exception.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment