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

NullPointerException

 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers

Quick question

woud that exception works only for ARRAY or if we assign array reference value to some undefined type and then we try to access it. would it also give the same exception in accessing undefined variable.

example class A {

int a;
}
class B {

int [] b = new int(1);

A me = new A();
me.a;
} //pardon for detail syntax liike main and all

- just wanna make sure cases when we get NullpointerException
- Thank you ranchers
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you run it ? Primitive instance variables like int are defaulted to 0
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi john ,

i know the defaults assignments for primmitives but wanted to know that what if we access the primmitive which is declared and we are assigning NULL array value in it.

thank
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NullPointerException is thrown when the reference can't find an object to refer, when you are assuming that it actually refers an object!
By the way, an int[] can hold only integers; not objects!
Read it as "an integer array" where [] stands for array and int stands for integer. Precisely, it is an array that holds integers - got it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic