• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

array access

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have something like this in main method:

and try to do


Exception in thread "main" java.lang.NullPointerException
is thrown.

My question is: why not null is printed?
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all for long the default value is 0 and not null.
Then you are trying to access a memory location which has not
yet been allocated thats the reason you get a nullpointer exception.
 
Andrei Nistor
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks' you were very fast!

...and indeed the memory is not yet allocated... but why is it NOT a compiler error?
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(a[1]); will give you null.

You have an int-Array a[] with 3 elements. The elements are again int-arrays but as you haven't initialized these arrays (by giving them a size) they are set to their default value -> null.
Now you are trying to get the element at position 1 of one of these "null-arrays" and that cannot work.

It's the same as trying:
int [] array;
System.out.println(array[1]);

Oops, have been to slow...

but why is it NOT a compiler error?


Good question
[ April 21, 2007: Message edited by: Sasha Ruehmkorf ]
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here You have two dimentional array. Now before some time i had read that java does not support multi dimentional array. If you see multidimentional array then it would be array of array.

Example : int[][] a = new int[3][];
Now here, a is a two dimentional array but the structure used is of one dimentional. I mean, a[0] will refer to another single dimentional array and that is not still decided when you are using System.out.println(XXX), same for a[1] and a[2].

Now if try this code:

Now can you think why a[0] gives output other then null, and a[1] and a[2] gives null.

If you try to remove the comment it will give an exception, i am sure you know the reason.


I hope this will help you little.
Thanks.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic