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 array... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array..." Watch "array..." New topic
Author

array...

Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
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
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


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

Joined: Aug 29, 2008
Posts: 580

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.


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


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 ]

SCJP 6
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks.Punit.
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

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

Joined: Sep 07, 2008
Posts: 265
Hi Punit,
Thanks for your expalnation. now i am clear with it.

Preetha
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: array...
 
Similar Threads
Searching Arrays (K&B p. 577)
ExamLab Q15
Searching Arrays and Collections
Doubt in Collections
Comparator's compare() method question