• Post Reply Bookmark Topic Watch Topic
  • New Topic

Null pointer Exception  RSS feed

 
Priya chek
Ranch Hand
Posts: 51
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i have a doubt that how this program throws Null Pointer Exception???

public class M10
{
static Integer x;
public static void main(String[] args) {

test(x);
}
static void test(int z)
{
int z1=5;
System.out.println(z+z1);
}
}
 
Ankit Dan
Ranch Hand
Posts: 47
Eclipse IDE Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

By default the Integer reference x will be assigned null value that is why your program is throwing NullPointerException.

Hope this helps!!
Ankit
 
Priya chek
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Thanks Ankit. But my doubt is what is the problem of using null reference as a parameter?
 
Ankit Dan
Ranch Hand
Posts: 47
Eclipse IDE Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This ll clear your doubts. java.lang.NullPointerException

Hope this helps!
Ankit
 
Roshan Lobo
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int is a primitive which with default value of 0;
Integer is an object with default value of null.
Null pointer exception occurs when you are trying to unbox the object to primitive.

First understand the difference between primitives and objects. Read boxing and unboxing concepts.
Try working the same example in java 1.4. You will be more clear.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!