New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
null.asInstanceOf[T] where T is a value type results in NullPointerException #8097
Comments
Imported From: https://issues.scala-lang.org/browse/SI-8097?orig=1 |
@lrytz said: scala> class C(val x: Int) extends AnyVal
defined class C
scala> var o: Any = _
o: Any = null
scala> o.asInstanceOf[C]
java.lang.NullPointerException
... 32 elided |
Markus Marvell (Marvell) said (edited on Mar 11, 2016 6:29:21 AM UTC): object ATest extends App {
def getX[T](constr: ⇒ T = null.asInstanceOf[T]): Unit = constr
getX()
} Discussion and more examples here: http://stackoverflow.com/questions/35245454/scala-by-name-parameter-with-default-null-throws-nullpointerexception |
@etorreborre said: case class V(s: Int) extends AnyVal
def concrete(v: V) = 1
def generic[T]: T = null.asInstanceOf[T]
// ok
generic[V]
// NPE
concrete(generic[V]) This is with Scala 2.11.7 |
@lrytz said: scala> class C(val x: Int) extends AnyVal { override def toString = "" + x }
defined class C
scala> null.asInstanceOf[C]
res3: C = 0
scala> (null: Null).asInstanceOf[C]
res4: C = 0
scala> (null: AnyRef).asInstanceOf[C]
java.lang.NullPointerException
... 30 elided
scala> (null: Any).asInstanceOf[C]
java.lang.NullPointerException
... 30 elided |
@lrytz said: scala> class C(val x: String) extends AnyVal
defined class C
scala> null.asInstanceOf[C]
java.lang.NullPointerException and scala> class C[T](val x: T) extends AnyVal
defined class C
scala> null.asInstanceOf[C[Int]]
java.lang.NullPointerException |
Cyrille Chépélov (cchepelov) said (edited on Aug 3, 2016 3:52:31 PM UTC): case class Foo(d: Double) extends AnyVal
null.asInstanceOf[Foo] // works case class Foo(d: Double) extends AnyVal
def newInst[T] = null.asInstanceOf[T]
newInst[Foo] // NPE But did not reproduce [~rytz]'s report under 2.12.0-M5: case class Bar(val x: String) extends AnyVal
null.asInstanceOf[Bar] // OK, returns Bar(null) case class Baz[T](val x: T) extends AnyVal
null.asInstanceOf[Baz[Int] // OK, returns Baz(null) case class Bat[T <: AnyVal](val x: T) extends AnyVal
null.asInstanceOf[Baz[Int] // OK-ish, returns Bat(null) (implying x was boxed?) |
While the direct route of
was fixed in #5866, the issue remains when Generics get involved:
The text was updated successfully, but these errors were encountered: