Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: null pointer exception with nested ? : syntax

  1. #1
    jamesd128 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default null pointer exception with nested ? : syntax

    Could someone please explain to me why executing this code results in a NullPointerException?

    String s = "fds";
    Integer t = s.equals("d") ? 1 : s.equals("fds") ? null : 2;

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: null pointer exception with nested ? : syntax

    One way to get the exception with that statement is if s has a null value.

    What happens if you replace the null with a number?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: null pointer exception with nested ? : syntax

    Quote Originally Posted by jamesd128 View Post
    Could someone please explain to me why executing this code results in a NullPointerException?

    String s = "fds";
    Integer t = s.equals("d") ? 1 : s.equals("fds") ? null : 2;
    The nature of the terniary operator is funny. I think it tries to change all possible results to the same type, and so I believe that the line causes an internal call to:

    Java Code:
          Integer.valueOf(null);
    which will cause a NPE.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: null pointer exception with nested ? : syntax

    Here's the javap output:
    Java Code:
    D:\JavaDevelopment\Testing\ForumQuestions8>javap -c TestCode11
    Compiled from "TestCode11.java"
    public class TestCode11 extends java.lang.Object{
    public TestCode11();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
    
    public static void main(java.lang.String[])   throws java.lang.Exception;
      Code:
       0:   ldc     #2; //String fds
       2:   astore_1
       3:   aload_1
       4:   ldc     #3; //String d
       6:   invokevirtual   #4; //Method java/lang/String.equals:(Ljava/lang/Object;
    )Z
       9:   ifeq    16
       12:  iconst_1
       13:  goto    36
       16:  aload_1
       17:  ldc     #2; //String fds
       19:  invokevirtual   #4; //Method java/lang/String.equals:(Ljava/lang/Object;
    )Z
       22:  ifeq    29
       25:  aconst_null
       26:  goto    33
       29:  iconst_2
       30:  invokestatic    #5; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Int
    eger;
       33:  invokevirtual   #6; //Method java/lang/Integer.intValue:()I
       36:  invokestatic    #5; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Int
    eger;
       39:  astore_2
       40:  iconst_0
       41:  invokestatic    #7; //Method java/lang/System.exit:(I)V
       44:  return

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: null pointer exception with nested ? : syntax

    Quote Originally Posted by Norm View Post
    Here's the javap output:
    LOL, I just did the same thing, and in fact it's the first time that I created a byte code file with javap. It appears to me that I'm correct, that Integer.intValue(...) is called which maps to Integer.valueOf(...) which is called on the null resulting in a NPE.

  6. #6
    jamesd128 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default Re: null pointer exception with nested ? : syntax

    The explanation that the terniary operator will try to take the valueOf on all the result types doesn't make sense though.

    First, Integer.valueOf(null) throws a NumberFormatException not a NPE.

    Second, that wouldn't explain why this line works fine:
    Integer t = s.equals("fds") ? null : 2;
    but this one doesn't:
    Integer t = s.equals("d") ? 1 : s.equals("fds") ? null : 2;

    The NPE is only thrown in the nested version.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: null pointer exception with nested ? : syntax

    As Norm and Fubarable pointed out, it's the intValue called on the null -- the result of auto-unboxing -- that gives rise to the NPE.

    As framed, the type returned from your nested ternary is interpreted by the compiler to be the primitive type int. This int is autoboxed to Integer on assignment.

    This doesn't throw an NPE since the type of the ternary value expression is Integer and no auto-unboxing is attempted.
    Java Code:
    Integer t = s.equals("d") ? Integer.valueOf(1) : s.equals("fds") ? null : Integer.valueOf(2);
    Neither does this:
    Java Code:
    Integer t = s.equals("d") ? Integer.valueOf(1) : s.equals("fds") ? null : 2;
    nor this:
    Java Code:
    Integer t = s.equals("fds") ? null : s.equals("d") ? 1 :  2;
    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. null pointer exception
    By bequick01 in forum New To Java
    Replies: 3
    Last Post: 04-28-2011, 08:31 PM
  2. Null Pointer Exception
    By jonytek in forum New To Java
    Replies: 5
    Last Post: 03-02-2011, 07:16 AM
  3. null pointer exception
    By marvelk in forum Advanced Java
    Replies: 8
    Last Post: 02-01-2011, 09:02 AM
  4. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 11:27 AM
  5. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 01:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •