• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException

 
Phoebe Song
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I had an assignment to convert a primitive int to an Integer object, then convert back, when I run I've got a NullPointerException. Can anyone help me find out why. Thank you.

public class ConvertInt {
int a = 100;
Integer i;

public Integer convertToInteger(){
i = new Integer(a);
return i;
}

public int convetToInt(){
a = i.intValue();
return a;
}

public static void main(String[] args) {
new convertInt().convertToInteger();
new convertInt().convetToInt();
}

}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem you have in your main method is that you are calling the two methods with different instances of ConvertInt.

The class has an instance method of type Integer called i. Initially when an instance of the class is created, it is initialized to null since you don't explicitly initiliaze it yourself.

In the first method, you use an instance of the class to call the method where you initialize i.

But in the second method, you are using another instance to call the method where you use i. In that second instance, i is null.

You should call the two methods with the same instance of the class.
[ April 04, 2006: Message edited by: Keith Lynn ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Phoebe,

You're creating two "ConvertInt" objects. Each one gets their own instance of the member variables "i" and "a", just as each person has their own pockets. When one object creates an Integer and stores it in "i", another ConvertInt is unaffected and its "i" remains null, just as putting a stone in your pocket doesn't automatically fill up your friend's pocket.

You need to call both those methods on the same ConvertInt object. Create it, store it in a variable in main(), then call the two methods via that variable.
 
Phoebe Song
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You guys rock! Thank you.
 
anji_java
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is where U can realise the importance of the keyword "static".
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by anji_java:
This is where U can realise the importance of the keyword "static".


Sorry to sound mean, but this is possibly the worst advice I've ever seen given out on JavaRanch. Making anything "static" here would be terrible, terrible style. The correct solution is to understand (as Phoebe now does) how objects work, and program accordingly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic