File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrappers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrappers" Watch "Wrappers" New topic


akash azal
Ranch Hand

Joined: Jan 31, 2009
Posts: 70

why this code gives nullPointerException .

We will keep things moving!!
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

The Integer you pass to doStuff is being boxed out (i.e converted into an int). To do so, Integer.intValue will be called on your variable, but because it is not initialized (it's null here), an NPE is thrown.

This would also happen if you'd call "int z = x;" without x being initialized.

[My Blog]
All roads lead to JavaRanch
akash azal
Ranch Hand

Joined: Jan 31, 2009
Posts: 70
Carl Cordova

Joined: Feb 02, 2008
Posts: 4
I attempted to take this code a step further by overloading the doStuff method and initializing the static wrapper. I was expecting the end value of x to be 10, but it is actually 5. I don't understand what is happening.

Here is the output:

$ java Box6
Value of i in doStuff(Integer) is : 10
Value after doStuff is : 5


Carl Cordova
Massimo Battestini

Joined: Nov 21, 2008
Posts: 11
What I believe is happening is that in the operation:

i = i + y;

it is unboxing i, then it adds y and finally re-box everything and assign it to i. But while re-boxing it creates a new object;

in this way i inside doStuff and i inside main will have a reference to two distinct objects.
Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

Well wrappers are immutable, so if you reassign a value to them, then a new object is created.

And this is just one part of the story. Since you are using the wrapper in a method call, so it will be passed as a reference pointer (this is what I call it). If it was a primitive, then there was no chance of change in the value of original variable as primitives are passed as value. But even if we pass a reference to the method, the original value of x will not change as in doStuff, a new object will be created o point to the value 10 and x will still point to the Integer object with the value 5. I hope it is clear...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Leonard Fischer
Ranch Hand

Joined: Apr 17, 2009
Posts: 47
I appreciate Ankit's most interesting observation about about Integer instances being immutable. I'd like to request some moreinfo about this, because I'm concerned about how to recognize when an object is immutable.

In the following code,

static Integer fn1(Integer i)
i = i + 1;
return i;
static Animal fn2(Animal a)
{ = "New name";
Animal b = a;
return b;

Consistent with what Ankit has said, fn1() does not change the value of the ARGUMENT i, since Integer is immutable. fn2() does change the value of the ARGUMENT a; the "name" field is "New name".

I would be most curious if anyone knows which classes are immutable, and which are not. Of course, String is immutable. I believe that Date is immutable. Can anyone provide a list of immutable classes? Is there a way to define your own class, and make it immutable?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Is there a way to define your own class, and make it immutable?

yes certainly its possible. This is good tutorial to start
I agree. Here's the link:
subject: Wrappers
It's not a secret anymore!