Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Multidimensional array

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the follwing code:
class try3
{
public static void main(String args[])
{
long[][] a1;
long[] a2[];
a2=new long[3][];//7
a1=a2;
System.out.println(a2[1][0]);//9
}
}

It gives NullPointerException due to line 9.
My question:
Doesn't the statement on line 7 initialise all array elements to their default value of 0? Why does it throw a NullPointerException?
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi dravid,
Array elements are initialized to their default values . its correct..
actually multidimensional arrays are the array of arrays.
take the following example,

int [][] a = new int[3] [3];

in the above we created array of size 3 . in that
a[0],a[1],[2] are points to other int array reference .

actually default value for array reference is null.so if u give

int [][] a = new int[3][];

means a[0],a[1] and a[2] will take their default values of reference ie null.

if u access the elments of array that points to null , it will throw null pointer exception .



S.D.Balasubramani,
SCJP 1.4,
 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Multidimensional array are array of array .

int[][] i = new int[2][1];

what it means is , i is refering to a one dimensional array ( size 2 ) of int array reference variable & that array reference is refering to a one dimentional array ( size 1 ) of int primitive variable .

So in your case , you have specified that a2 is refering to a one dimensional array ( size 3 ) of long array reference variable and this all long reference variable are refereing to an array of long primitive but what is the size of this array ??

hope it is clear .
[ March 21, 2005: Message edited by: rathi ji ]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Array construction in line 7 is not fully qualified.

line 7: int []a[]=new int [3][];
In above stmt, length of the deeply nested array is omitted. Hence this is left unconstructed. So, when u access the element a[2][0] in your code, the compile throw run-time exception, NullPointerException.

Replace line 7 with int []a[]=new int [3][3], the code works fine.

Hope you understand.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I actually agree with Balasubramani Dharmalingam, because when declaring/constructing arrays it is not necessary to also define the length of the second dimension in an array (for ex: int [][] a2 = new int [3][] .

Both your arrays are local (a1 doesnt really matter since you are not using it anywhere) and for local variables, it is necessary that you initialize it to something (be it null!! too, doesnt matter). I think that is the reason for the error.

Sabeer's solution works, but according to me that is not the actual reason for the error.

More feedback welcome!

Thanks

-Kinjal
[ March 21, 2005: Message edited by: kiennjal shah ]
 
Balasubramani Dharmalingam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually arrays are considered as an object so it will take default value as null.Take an example,
class Sample{
static int [] a;

public static void main(String [] args){
System.out.println(a);
}
}

It will prints null.

If u define the two dimensional array as follows,
int [][] a = new int[3][3];
(it is equivalent to,
int[][] a = new int[3][3];
a[0] = new int[3];
a[1] = new int[3];
a[2] = new int[3];
)


|-----|
| | [some single dimensional array of length 3]
|-----|
a[0] ------| |
|-----|
| |
|-----|


|-----|
| | [some single dimensional array of length 3]
|-----|
a[1] ------| |
|-----|
| |
|-----|



|-----|
| | [some single dimensional array of length 3]
|-----|
a[2] ------| |
|-----|
| |
|-----|


suppose if u didnt specify the second dimension means

default value for array object will be assigned to a[0] ,

a[1] and a[2].(ie null);


a[0]-------> null

a[1]-------> null

a[2]-------> null


S.D.Balasubramani.
 
Balasubramani Dharmalingam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry friends allignment had changed in previous reply,
Actually arrays are considered as an object so it will take default value as null.Take an example,
class Sample{
static int [] a;

public static void main(String [] args){
System.out.println(a);
}
}

It will prints null.

If u define the two dimensional array as follows,
int [][] a = new int[3][3];
(it is equivalent to,
int[][] a = new int[3][3];
a[0] = new int[3];
a[1] = new int[3];
a[2] = new int[3];
)



a[0]---------> [some single dimensional array of size 3]



a[1] ---------> [some single dimensional array of size 3]




a[2] ---------> [some single dimensional array of size 3]



suppose if u didnt specify the second dimension means

default value for array object will be assigned to a[0] ,

a[1] and a[2].(ie null);


a[0]-------> null

a[1]-------> null

a[2]-------> null

if try to access members of object by using null means it will throw NullPointerException thats why it throws NullPointerException .



S.D.Balasubramani.
SCJP1.4
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic