This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException

 
Sujatha Musunuri
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. public class Boxer1 {
2. Integer i;
3. int x;
4. public Boxer1(int y) {
5. x=i+y;
6. System.out.println(x);
7. }
8. public static void main(String[] args) {
9. new Boxer1(new Integer(4));
10. }
11. }

The Ans for this is 'NullPointerException' at runtime. Can some one let me know why is it giving that Exception?
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you didn't initialize the Integer i, by default i will be null. Hence, you get the NullPointerException when trying to perform the addition.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there Musu Musu,

Please help us out with two things:

1 - is that your real name? If not, please change your display name to be your first and last name. That's the policy at the ranch and it keeps things friendlier.

2 - it's great to post mock questions to discuss, but please indicate where you got the question when you post it - it's kind to the autor of the qeustion, and it can help us help you.

Thanks,

Bert
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Musu,
Auto-unboxing a null reference will always cause a NullPointerException.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic