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 Multidimensional array 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 "Multidimensional array" Watch "Multidimensional array" New topic
Author

Multidimensional array

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
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

Joined: Dec 06, 2004
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,


Balasubramani SD,<br />SCJP 1.4,SCWCD 1.4,SCJP 5.0<br /><a href="http://sd.balasubramani.googlepages.com" target="_blank" rel="nofollow">www.sd.balasubramani.googlepages.com</a>
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
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

Joined: Mar 15, 2005
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

Joined: Jun 17, 2004
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

Joined: Dec 06, 2004
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

Joined: Dec 06, 2004
Posts: 116
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multidimensional array
 
Similar Threads
array doubt
local arrays
Arrays
passing arrays
K&B Exam - incorrect answer?