Results 1 to 7 of 7
- 03-05-2012, 01:30 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
- 03-05-2012, 01:37 AM #2
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?
-
Re: null pointer exception with nested ? : syntax
- 03-05-2012, 01:54 AM #4
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
-
Re: null pointer exception with nested ? : syntax
- 03-05-2012, 04:32 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
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.
- 03-05-2012, 11:14 AM #7
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.Neither does this:Java Code:Integer t = s.equals("d") ? Integer.valueOf(1) : s.equals("fds") ? null : Integer.valueOf(2);nor this:Java Code:Integer t = s.equals("d") ? Integer.valueOf(1) : s.equals("fds") ? null : 2;dbJava Code:Integer t = s.equals("fds") ? null : s.equals("d") ? 1 : 2;Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
null pointer exception
By bequick01 in forum New To JavaReplies: 3Last Post: 04-28-2011, 08:31 PM -
Null Pointer Exception
By jonytek in forum New To JavaReplies: 5Last Post: 03-02-2011, 07:16 AM -
null pointer exception
By marvelk in forum Advanced JavaReplies: 8Last Post: 02-01-2011, 09:02 AM -
Null Pointer Exception
By ScKaSx in forum New To JavaReplies: 1Last Post: 01-24-2009, 11:27 AM -
Null Pointer Exception
By Jacinth in forum New To JavaReplies: 4Last Post: 01-22-2009, 01:47 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks