Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.NullPointerException

 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya solved. thank you.
 
Ankush Kaundal
Ranch Hand
Posts: 36
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Design error. Every field ought to be initialised to establish the class invariant in the constructor.
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya. i changed the code it's not giving any error now.
 
shaileshkumar mistry
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic