Skip to content
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

Open
scabug opened this issue Dec 23, 2013 · 7 comments
Open

Comments

@scabug
Copy link

@scabug scabug commented Dec 23, 2013

While the direct route of

case class Foo(d: Double) extends AnyVal
null.asInstanceOf[Foo]

was fixed in #5866, the issue remains when Generics get involved:

scala> case class Foo(d: Double) extends AnyVal
defined class Foo

scala> def newInst[T] = null.asInstanceOf[T]
newInst: [T]=> T

scala> newInst[Foo]
java.lang.NullPointerException
  ... 32 elided
@scabug
Copy link
Author

@scabug scabug commented Dec 23, 2013

Imported From: https://issues.scala-lang.org/browse/SI-8097?orig=1
Reporter: @soc
Affected Versions: 2.10.3, 2.11.0-M7, 2.11.8, 2.12.0-M5
See #7396, #5866

@scabug
Copy link
Author

@scabug scabug commented Jul 3, 2014

@lrytz said:
Here's another example:

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
@scabug
Copy link
Author

@scabug scabug commented Feb 7, 2016

Markus Marvell (Marvell) said (edited on Mar 11, 2016 6:29:21 AM UTC):
Here is another example with unexpected NPE:

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

@scabug
Copy link
Author

@scabug scabug commented Apr 8, 2016

@etorreborre said:
I have a slightly different case:

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

@scabug
Copy link
Author

@scabug scabug commented May 18, 2016

@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
@scabug
Copy link
Author

@scabug scabug commented May 18, 2016

@lrytz said:
also

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
@scabug
Copy link
Author

@scabug scabug commented Aug 3, 2016

Cyrille Chépélov (cchepelov) said (edited on Aug 3, 2016 3:52:31 PM UTC):
reproduced in 2.11.8 and 2.12.0-M5:

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?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant