This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception in thread "main" java.lang.NullPointerException" Watch "Exception in thread "main" java.lang.NullPointerException" New topic

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.

Exception in thread "main" java.lang.NullPointerException

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

here is the code

Divya Janyavula

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

Joined: Oct 13, 2005
Posts: 37913
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

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%
wood burning stoves
subject: Exception in thread "main" java.lang.NullPointerException
Similar Threads
Collections - Arrays.asList() / List.add()
Need to create a similar functionality like System.out.println
static problem...
importing other classes and using classpath