File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

nullPointerException

basha khan
Ranch Hand

Joined: Jan 18, 2002
Posts: 87
plz tell me how nullpointerException is occuring?.
how it related to arrays?
int [][]arr={ {1,2},null}
arr[1][0]=10;//nullpointer or arrayindexException??
what will arr[1].length will return ?&why?
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
If you look at the initialization, you haven't allocated anything for row 1, so arr[1][0] doesn't point to a valid reference.
Under this condition the statement a[1].length doesn't make any sense either and will cause an exception to be thrown (not sure if it's a null pointer exception or array out of bound exception, anyone?)
[ January 24, 2002: Message edited by: Rajinder Yadav ]

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
basha,
We'd like to read the Javaranch Naming Policy and register again.
Thank you for your cooperation.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
flynn xi
Greenhorn

Joined: Jan 14, 2002
Posts: 8
I have tried your code. These two places both throw NullPointerException.
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
true in the declaration second row is null.
but say we now allocate like
a[1][0]=10; and a[1]a[1]=15;
so now the second row is there and members are allocated. but still it gives NullPointerException.
why ? or is it that the declaration is the final word and it shall always remain null ? can someone let us know....

Originally posted by Rajinder Yadav:
If you look at the initialization, you haven't allocated anything for row 1, so arr[1][0] doesn't point to a valid reference.
Under this condition the statement a[1].length doesn't make any sense either and will cause an exception to be thrown (not sure if it's a null pointer exception or array out of bound exception, anyone?)
[ January 24, 2002: Message edited by: Rajinder Yadav ]
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
HI
I think
arr[1][0] is equal to null, i.e
arr[1][0] = null;
now array at arr[1][0] does not point to any Array object ... it is null.
hence when we try to allocate anything it throws NPE.
To assign any thing first you have to do :
arr[1] = new int[3];
now you can get arr[1].length
as now it contains refernce to an object and which has a legth of 3
CMIW
CMIW


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
ok. just to confirm that when arrays are declared and instantiated, the length of array in terms of rows and columns cannot change.
right ? they are fixed after they have been declared. Though one can manipulate member values but one cannot add new members.

Originally posted by ravish kumar:
HI
I think
arr[1][0] is equal to null, i.e
arr[1][0] = null;
now array at arr[1][0] does not point to any Array object ... it is null.
hence when we try to allocate anything it throws NPE.
To assign any thing first you have to do :
arr[1] = new int[3];
now you can get arr[1].length
as now it contains refernce to an object and which has a legth of 3
CMIW
CMIW
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
just to confirm that when arrays are declared and instantiated, the length of array in terms of rows and columns cannot change.
right ? they are fixed after they have been declared. Though one can manipulate member values but one cannot add new members.

That's right. Once you've declared an array, you can not change its length. You can, however, leave parts of a multi-dimensional array as null and then fill them in when you know the proper length. This isn't really adding new elements to the array, but it does offer some control over the size of the array.
For example, say you're going to read a number of files and store the contents into an array. We'll assume that we can look into one file to get the number of files and names of the files that we'd like to read in. Since it doesn't relate to this question, we'll assume that the file contains a message saying that we'll need to read in 3 files, "foo.txt," "bar.txt," and "baz.txt." Now, to create a properly sized array, you might do something like this:

Using techniques like this, you can better control the size of your array but, you're right, once an array's length has been set, it can not be changed.
Corey


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: nullPointerException