This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# Multidimensional array

Kedar Dravid
Ranch Hand
Posts: 333
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?

Balasubramani Dharmalingam
Ranch Hand
Posts: 116
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,

ankur rathi
Ranch Hand
Posts: 3830
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 ]

sabeer
Ranch Hand
Posts: 41
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.

kiennjal shah
Ranch Hand
Posts: 31
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
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
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