File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

Phoebe Song
Ranch Hand

Joined: Jan 17, 2002
Posts: 54
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Phoebe Song
Ranch Hand

Joined: Jan 17, 2002
Posts: 54
You guys rock! Thank you.
anji_java
Greenhorn

Joined: Apr 05, 2006
Posts: 5
This is where U can realise the importance of the keyword "static".
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException
 
Similar Threads
coding doubts of static methods
[URLyBird]Why not come in and debate on my code of find?
exception
Unable to deploy an EJB.
Integer.toString acting strangely with leading zeroes