• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array...

 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i got it from Devaka cooray's practice exam 3.

class strex
{
public static void main(String args[])
{

String[] sa[]=new String[2][];
sa[0]=new String[]{"A","B","C","D"};
for(String[] s:sa){
System.out.print(s[1]+", ");
System.out.print(s[2]+", ");
}
}
}

could anyone explain me the for loop here and it throws NullPointerException.
I am confused with the assignment of String[] s.

Thanks in advance
Preetha
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String[] sa[]=new String[2][];

You are declaring an array of arrays of Strings. You are also initializing the array of arrays with an array of array of strings of size two. However, you are not initializing the string arrays -- they will be set to null.

sa[0]=new String[]{"A","B","C","D"};

This initializes the first of the array of strings to an array with the four letters. The other array is still null.

Henry
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The answer is A, B, C, D, null, null, null, null.
The only difference is I have specified the dimensiions as [2][4] in Line1.But now why no NULLPOINTER EXCEPTION?
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String[] sa[]=new String[][];

The above means that sa is a 2d array of strings,whose size is not know.
Memory is not allocated.
So JVM dont know what to print and from WHERE to print.

String[] sa[]=new String[2][4];// Line1

This means that sa is a 2d array of size [2][4].
Now JVM allocates the memory,with null as its contents.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


See Arun here

String[] sa[]=new String[2][];

means now you have

sa[0]
and
sa[1]


and
sa[0]=new String[]{"A","B","C","D"};
means

sa[0]=|"A"|"B"|"C"|"D"|
0 1 2 3

and

sa[1]=null;

means
sa[0][0]="A"
sa[0][1]="B"
sa[0][2]="C"
sa[0][3]="D"


so
sa[1][0], sa[1][1] cannot be called as sa[1]=null. If you try to call
sa[1][0], you are calling null[0] that is NullPointerException.

So now

for(String[] s:sa){
/*
first time sa[0] in sa[2][]
second time sa[1] in sa[2][]
*/
System.out.print(s[1]+", ");
/*first time it will come for sa[0] and will call sa[0][1] it is ok
in second loop it will come for sa[1] and will call sa[1][1] means null[1] so You are calling [] operator on null that is NullPointerException.*/
System.out.print(s[2]+", ");
}

[ December 16, 2008: Message edited by: punit.singh ]
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.Punit.
 
nav katoch
Ranch Hand
Posts: 246
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Punit. I appreciate it as I am still not good at multidimensional arrays. So, I am catching up.

Naveen Katoch
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Punit,
Thanks for your expalnation. now i am clear with it.

Preetha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic