• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

array...

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.Punit.
 
Ranch Hand
Posts: 246
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Punit,
Thanks for your expalnation. now i am clear with it.

Preetha
 
reply
    Bookmark Topic Watch Topic
  • New Topic