aspose file tools*
The moose likes Beginning Java and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception in thread "main" java.lang.NullPointerException" Watch "Exception in thread "main" java.lang.NullPointerException" New topic
Author

Exception in thread "main" java.lang.NullPointerException

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
hi all,

Good morning. I am developing an program to call integer from one method to another. in this i am getting the output window. but getting an exception.
Error:

Exception in thread "main" java.lang.NullPointerException

if i type yes or Yes. How to solve this. ??

here is the code



Divya Janyavula
Greenhorn

Joined: May 24, 2011
Posts: 19
Post the exact location of your exception in the code or post the complete stack trace. Also check if your strYes variable has null value.

Also you have not called the program1 method and intMoney variable value will be null. This also might be the problem
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
ya solved. thank you.
Ankush Kaundal
Ranch Hand

Joined: May 12, 2011
Posts: 36

The exception is coming because your variable intMoney is not initialized as you have not called the static method program1() so solution is to either call this method or put the code on line 09 in static block rather than in method.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38840
    
  23
Design error. Every field ought to be initialised to establish the class invariant in the constructor.
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
ya. i changed the code it's not giving any error now.
shaileshkumar mistry
Greenhorn

Joined: Dec 19, 2011
Posts: 19
Hi Deepika

As you can see that Integer is a wrapper class so the default value when class initialized your intMoney will be null,

and when you do Integer i =intMoney the i also will reference to a null object.

so when you do an Integer x=i-10 the value of i is null , the wrapper object's can not convert null to 0 or vice versa so it will give null pointer exception.


Thanks and Regards

OCPJP 100%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in thread "main" java.lang.NullPointerException