• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Boxing/Unboxing

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

Have you tried to run the program in page 237 exam watch? I'm not getting the result as mentioned there. The code is


and says "This code compiles fine, but the JVM throws a NullPointerException
when it attempts to invoke doStuff(x), because x doesn�t refer to an Integer object, so there�s no value to unbox."

But I couldn't compile this code. Are you getting the same? If so what would be the cause? Autoboxing not working?

Thanks,
Rex
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what error you are getting? Which version of jdk you are using?
 
Rex Isaacs
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sahid Khan:
what error you are getting? Which version of jdk you are using?



The error I'm getting is

"Boxing2.java:4: doStuff(int) in Boxing2 cannot be applied to (java.lang.Integer)

doStuff(x);
^
1 error"

java -version gives

"java version "1.5.0_11"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode)"
 
Sahid Khan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah.. that is strange! It compiles fine with me! While compiling, are you using any argument called source? In fact for me the error comes only when I use javac -source 1.4.

Can you try with javac -source 1.5
 
Rex Isaacs
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not using -source with javac. This is what I did

C:\SCJP Samples\Chapter3>java -version
java version "1.5.0_11"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode)

C:\SCJP Samples\Chapter3>javac Boxing2.java
Boxing2.java:4: doStuff(int) in Boxing2 cannot be applied to (java.lang.Integer)

doStuff(x);
^
1 error



I'm not sure how to use -source, can you please tell me?
 
Sahid Khan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not know why this error is coming for you. With version >= 1.5 this autoboxing should work. And for you version is showing 1.5.
To use source in place of

C:\SCJP Samples\Chapter3>javac Boxing2.java


use

C:\SCJP Samples\Chapter3>javac -source 1.5 Boxing2.java

 
Rex Isaacs
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sahid,

Its a fault by myself, but I couldn't understand that. Eventhough the -version shows java 1.5, the classpath was pointing to the jdk1.4, which I understood only when I checked the environment variable.

Its solved now and the program is working.

Thanks Sahid

-Rex
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic