| 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: 30
|
|
|
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: 32644
|
|
|
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%
|
 |
 |
|
|
subject: Exception in thread "main" java.lang.NullPointerException
|
|
|