Help coderanch get a
new server
by contributing to the fundraiser
  • 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

ExamLab Q15

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am confused about the output of this code



The output is
B, C, followed by NullPointerException (I ran the code and I get this answer)

I think it should be A, B, C, D followed by NullPointerException
i just don't get it.....why A and D are not printed

Thank in advance.

Anu
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anu,

In the loop you are saying: For each array of String in sa, print the second and the third elements (array indexes start at 0.)
In the second iteration you get a NPE because sa[1] is null.
 
Anu Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I see it
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:Anu,

In the loop you are saying: For each array of String in sa, print the second and the third elements (array indexes start at 0.)
In the second iteration you get a NPE because sa[1] is null.



Very Tricky!
Thank you for the explaination.
-Fritz
 
Ranch Hand
Posts: 121
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruben Soto,

Still it is confusing,

can you explain more in detail.
Thanks in advance.

Cheers,
Suresh.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


why it is still NullPointerException is thrown???
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@suresh dasari, the code is explained line by line


@Chandana Garlapati
 
Fritz Guerilus
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chandana Garlapati wrote:

why it is still NullPointerException is thrown???



It's the for loop that is confusing you.
Normally the for loop in a 1D array is like this:
String [] sa = new String [] {"A","B","C","D"};
for(String s : sa) { //for each String object in the 1D array

But we're dealing with a 2D array, array of arrays:
String[] sa[]=new String[2][];
OR
String sa[][]=new String[2][];

The 1st array sa contains 2 elements, each of which referr to an array--sa[0], sa[1].
sa[0] has been initialized by the statement:
sa[0]=new String[]{"A","B","C","D"};
The 2nd array sa[1] is still null becuase it hasn't been initialized

The for loop in the code is looping through a 2D array and that is why it is:
for(String[] s:sa){ //for each array of String in the 2D array sa.

Here is the best part as stated by Ruben Soto:

In the loop you are saying: For each array of String in sa, print the second and the third elements (array indexes start at 0.)
In the second iteration you get a NPE because sa[1] is null.



That is why 'A' and 'D' aren't printed. They are the 1st and 4th element of the array.
This was tricky question.
I suggest you do research on 2D arrays.
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood clearly..

Thanks a lot Gaurav Joshi and Fritz Guerilus
 
suresh dasari
Ranch Hand
Posts: 121
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gaurav and Fritz.

now I understood clearly.
 
I am a man of mystery. Mostly because of this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic