Home Ask Login Register

Developers Planet

Your answer is one click away!

Markus Marvell February 2016

Scala by-name parameter with default null throws NullPointerException

The following snippet throws NullPointerException. Is it expected and normal behavior of Scala?

object ATest extends App {
    def getX[T <: X](constr: ⇒ T = null.asInstanceOf[T]): Unit = {
        constr
    }
    getX()
}
class X

Generated (decompied) Java code from snippet:

public final class ATest {
public static void main(String[] arrstring) {
    ATest$.MODULE$.main(arrstring);
}
public static void delayedInit(Function0<BoxedUnit> function0) {
    ATest$.MODULE$.delayedInit(function0);
}
public static String[] args() {
    return ATest$.MODULE$.args();
}
public static void scala$App$_setter_$executionStart_$eq(long l) {
    ATest$.MODULE$.scala$App$_setter_$executionStart_$eq(l);
}
public static long executionStart() {
    return ATest$.MODULE$.executionStart();
}
public static void delayedEndpoint$test$ATest$1() {
    ATest$.MODULE$.delayedEndpoint$test$ATest$1();
}
public static <T extends X> T getX$default$1() {
    return ATest$.MODULE$.getX$default$1();
}
public static <T extends X> void getX(Function0<T> function0) {
    ATest$.MODULE$.getX(function0);
}
}


public final class ATest$ implements App {
public static final ATest$ MODULE$;
private final long executionStart;
private String[] scala$App$$_args;
private final ListBuffer<Function0<BoxedUnit>> scala$App$$initCode;

public static {
    new test.ATest$();
}
public long executionStart() {
    return this.executionStart;
}
public String[] scala$App$$_args() {
    return this.scala$App$$_args;
}
public void scala$App$$_args_$eq(String[] x$1) {
    this.scala$App$$_args = x$1;
}
public ListBuffer<Function0<BoxedUnit>> scala$App$$initCode() {
    return this.scala$App$$initCode;
}
public void scala$App$_setter_$executionStart_$eq(long x$1) {
    this.executionStart = x$1;
}
public void scala$App$_setter_$scala$App$$initCode_$eq(ListBuffer x$1) {
    this.scala$App$$initCode = x$1;
}
public String[]         

Answers


dth February 2016

So I have to revise my answer a bit.

What triggers the NPE is however clear from the byte code, but not the reverse compiled Java code. Byte code has more features than Java code, the important one being, that you can have two methods that differ only in the return type and do different things.

So lets first look at the stack trace:

at ATest$$anonfun$1.apply(Test.scala:7)
at ATest$.getX(Test.scala:5)
at ATest$.delayedEndpoint$ATest$1(Test.scala:7)
at ATest$delayedInit$body.apply(Test.scala:3)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:383)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at ATest$.main(Test.scala:3)
at ATest.main(Test.scala)

So the method where it all goes wrong is ATest$$anonfun$1.apply

Lets look at that:

public final scala.runtime.Nothing$ apply();
Code:
   0: getstatic     #19                 // Field ATest$.MODULE$:LATest$;
   3: invokevirtual #23                 // Method ATest$.getX$default$1:()LX;
   6: checkcast     #25                 // class scala/runtime/Nothing$
   9: areturn

public final java.lang.Object apply();
Code:
   0: aload_0
   1: invokevirtual #30                 // Method apply:()Lscala/runtime/Nothing$;
   4: athrow

The first thing we notice is there are two Methods called apply, so which one is called (the athrow is a hint...) Well, lets look at the method calling it:

public <T extends X> void getX(scala.Function0<T>);
Code:
   0: aload_1
   1: invokeinterface #62,  1  // InterfaceMethod scala/Function0.apply:()Ljava/lang/Object;
   6: pop
   7: return

So, we are

Post Status

Asked in February 2016
Viewed 3,668 times
Voted 4
Answered 1 times

Search




Leave an answer


Quote of the day: live life