File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " NullPointerException" Watch " NullPointerException" New topic


chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
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();
} //pardon for detail syntax liike main and all

- just wanna make sure cases when we get NullpointerException
- Thank you ranchers
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

Why dont you run it ? Primitive instance variables like int are defaulted to 0

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
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.

Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

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?
I agree. Here's the link:
subject: NullPointerException
It's not a secret anymore!