Skip to main content



      Home
Home » Newcomers » Newcomers » Autoboxing and Nullpointer
Autoboxing and Nullpointer [message #524083] Tue, 30 March 2010 11:44 Go to next message
Eclipse UserFriend
Hello,

could someone please explain me, why the following snippet produces a NullPointerException:

public class Main {

public static void main(String[] args) {

System.out.println(getDouble());

Double d1 = true ? getDouble() : new Double(0);
System.out.println(d1);

Double d2 = true ? getDouble() : 0;
System.out.println(d2);
}

public static Double getDouble() {

return null;
}
}

The output is:

null
null
Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:9)


Apparently it has to do with autoboxing, but is this the right behaviour?
I'm using Eclipse Galileo for RCP/Plug-in Developers, Build id: 20090920-1017


Thank you in advance,
Rudi
Re: Autoboxing and Nullpointer [message #524183 is a reply to message #524083] Tue, 30 March 2010 21:22 Go to previous messageGo to next message
Eclipse UserFriend
Rudi wrote:
> Hello,
>
> could someone please explain me, why the following snippet produces a
> NullPointerException:
>
> public class Main {
>
> public static void main(String[] args) {
>
> System.out.println(getDouble());
>
> Double d1 = true ? getDouble() : new Double(0);
> System.out.println(d1);
>
> Double d2 = true ? getDouble() : 0;
> System.out.println(d2);
> }
>
> public static Double getDouble() {
>
> return null;
> }
> }
>
> The output is:
>
> null
> null
> Exception in thread "main" java.lang.NullPointerException
> at Main.main(Main.java:9)
>
>
> Apparently it has to do with autoboxing, but is this the right behaviour?
> I'm using Eclipse Galileo for RCP/Plug-in Developers, Build id:
> 20090920-1017
>
>
> Thank you in advance,
> Rudi

The ternary operator (?:) in Java is notorious for having problems with mixed or
ambiguous types. In this case the problem is that 0 is not a Double, it's an
int; the type of the expression "true ? getDouble() : 0" is ambiguous, it's not
clear whether 0 should be cast to a Double or the Double should be cast to an int.

You can read more about this issue in the Java Puzzlers book, info at
http://www.javapuzzlers.com/ .
Re: Autoboxing and Nullpointer [message #524192 is a reply to message #524183] Wed, 31 March 2010 02:54 Go to previous messageGo to next message
Eclipse UserFriend
Hi Walter,

thank you for the link to the book, i know Joshua Bloch from his excellent book "Effective Java".

Best regards,
Rudi
Re: Autoboxing and Nullpointer [message #524320 is a reply to message #524083] Wed, 31 March 2010 05:26 Go to previous message
Eclipse UserFriend
On 3/30/2010 9:44 AM, Rudi wrote:
> Hello,
>
> could someone please explain me, why the following snippet produces a
> NullPointerException:
>
> public class Main {
>
> public static void main(String[] args) {
>
> System.out.println(getDouble());
>
> Double d1 = true ? getDouble() : new Double(0);
> System.out.println(d1);
>
> Double d2 = true ? getDouble() : 0;
> System.out.println(d2);
> }
>
> public static Double getDouble() {
>
> return null;
> }
> }
>
> The output is:
>
> null
> null
> Exception in thread "main" java.lang.NullPointerException
> at Main.main(Main.java:9)
>
>
> Apparently it has to do with autoboxing, but is this the right behaviour?
> I'm using Eclipse Galileo for RCP/Plug-in Developers, Build id:
> 20090920-1017
>
>
> Thank you in advance,
> Rudi

While the ternary operator in Java has understandable problems for which
I lodge no particular complaint (despite being an old C guy who uses
this construct copiously), in this case, it's the null returned from
getDouble() that leads to the exception and not the ternary operation's
innate grumpiness, no?

Russ
Previous Topic:Adding checkbox in GridTree Viewer
Next Topic:Quick Help please ---- turning DEBUG statements OFF
Goto Forum:
  


Current Time: Sun Jun 08 10:54:21 EDT 2025

Powered by FUDForum. Page generated in 0.13212 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top