This page is no longer maintained — Please continue to the home page at www.scala-lang.org

null match no NullPointerException

3 replies
thinkingmind
Joined: 2011-12-29,
User offline. Last seen 42 years 45 weeks ago.

Why the following code won't throw a NullPointerException?

null match {
case null => println("null")
case _ => println("none")
}

-Ramesh

Ishaaq Chandy
Joined: 2009-02-16,
User offline. Last seen 42 years 45 weeks ago.
Re: null match no NullPointerException
why should it?

On 9 January 2012 12:46, thinkingmind <ramesh [dot] mandaleeka [at] gmail [dot] com> wrote:
Why the following code won't throw a NullPointerException?

null match {
   case null => println("null")
   case _ => println("none")
}

-Ramesh

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: null match no NullPointerException
Because null is never dereferenced.  Here's the bytecode for a method that does that match:

public void f();
  Code:
   0:    aconst_null
   1:    astore_1
   2:    aconst_null
   3:    pop
   4:    aconst_null
   5:    ifnonnull    19
   8:    getstatic    #12; //Field scala/Predef$.MODULE$:Lscala/Predef$;
   11:    ldc    #14; //String null
   13:    invokevirtual    #18; //Method scala/Predef$.println:(Ljava/lang/Object;)V
   16:    goto    27
   19:    getstatic    #12; //Field scala/Predef$.MODULE$:Lscala/Predef$;
   22:    ldc    #20; //String none
   24:    invokevirtual    #18; //Method scala/Predef$.println:(Ljava/lang/Object;)V
   27:    return

Leaving aside the excessive aconst_nulling at the top, note that the entire match gets translated into the "ifnonnull 19" after loading the appropriate value (i.e. null); it then goes through the appropriate print.

That's exactly what the match asks for, anyway, and that's what you get.  Convenient, isn't it!

  --Rex

On Sun, Jan 8, 2012 at 8:46 PM, thinkingmind <ramesh [dot] mandaleeka [at] gmail [dot] com> wrote:
Why the following code won't throw a NullPointerException?

null match {
   case null => println("null")
   case _ => println("none")
}

-Ramesh

thinkingmind
Joined: 2011-12-29,
User offline. Last seen 42 years 45 weeks ago.
Re: null match no NullPointerException
Hi Rex,

Thanks much for the detailed explanation.

Regards,
Ramesh

On Sun, Jan 8, 2012 at 9:02 PM, Rex Kerr <ichoran [at] gmail [dot] com> wrote:
Because null is never dereferenced.  Here's the bytecode for a method that does that match:

public void f();
  Code:
   0:    aconst_null
   1:    astore_1
   2:    aconst_null
   3:    pop
   4:    aconst_null
   5:    ifnonnull    19
   8:    getstatic    #12; //Field scala/Predef$.MODULE$:Lscala/Predef$;
   11:    ldc    #14; //String null
   13:    invokevirtual    #18; //Method scala/Predef$.println:(Ljava/lang/Object;)V
   16:    goto    27
   19:    getstatic    #12; //Field scala/Predef$.MODULE$:Lscala/Predef$;
   22:    ldc    #20; //String none
   24:    invokevirtual    #18; //Method scala/Predef$.println:(Ljava/lang/Object;)V
   27:    return

Leaving aside the excessive aconst_nulling at the top, note that the entire match gets translated into the "ifnonnull 19" after loading the appropriate value (i.e. null); it then goes through the appropriate print.

That's exactly what the match asks for, anyway, and that's what you get.  Convenient, isn't it!

  --Rex

On Sun, Jan 8, 2012 at 8:46 PM, thinkingmind <ramesh [dot] mandaleeka [at] gmail [dot] com> wrote:
Why the following code won't throw a NullPointerException?

null match {
   case null => println("null")
   case _ => println("none")
}

-Ramesh


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland