• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

nullPointerException

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
basha,
We'd like to read the Javaranch Naming Policy and register again.
Thank you for your cooperation.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried your code. These two places both throw NullPointerException.
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]

 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic