• 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

Enhanced for question

 
Greenhorn
Posts: 6
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
New comer here. I'm preparing 1z0-803 exam. I got a question about the output of following code.


The output is nullpointer exception. Why not print "-rgy" then print the nullpointer exception?
Thanks a lot!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Qu wrote:
The output is nullpointer exception. Why not print "-rgy" then print the nullpointer exception?



Your main thread never reaches the code after the for-loop. The exception causes the exit out of the main() method (remember that exceptions are thrown).

Henry
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James Qu,

First of all, a warm welcome to CodeRanch!

James Qu wrote:The output is nullpointer exception. Why not print "-rgy" then print the nullpointer exception?


There are 2 reasons:
  • the first one is already mentioned by Henry: the NullPointerException is thrown before the print-statement is reached and therefore nothing will be printed but the (runtime) exception will be thrown. This can easily be fixed by moving System.out.println(o); inside the for loop: either before or after the switch
  • the second reason is that if you don't use break statements in a case, you'll have a fall-through. Meaning: when a case label is matched, all subsequent cases (even the ones which don't match) will be executed as well. So for example, with the 1st string "RED", the second case label ("red") is matched and that one is executed but because there is no break the next case ("green") is executed as well. So after the 1st iteration, you already have "-rg" assigned to reference variable o. This one is easy to fix as well: just add break; as the last statement in each of the cases.


  • Hope it helps!
    Kind regards,
    Roel
     
    Marshal
    Posts: 8857
    637
    Mac OS X VI Editor BSD Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What Roel just explained, you could read about it also in Java language specification here (<- link). Look for "Example 14.11-1. Fall-Through in the switch Statement".

    Be aware, that could be difficult to read it, but there you can find actually all Java language corner cases explained.
     
    James Qu
    Greenhorn
    Posts: 6
    Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You guys are awesome! Appreciate!
     
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    My, that code is badly formatted. You should spread it out. You had your cases all squashed together on one line where they are difficult to read. It should have looked like this:-
    You should always add a comment whenever you have fallthrough in a switch otherwise people reading the code will assume you have made a mistake and add break; statements.

    The reason for the Exception is that you declared a 4‑element array and only filled 3 of its spaces; sa[3] remains as null. One way to get rid of the null is to change the declaration to declaration and initialisation with an array initialiser. Your code will now look like this:-Look at line 4. Note you only get one line of output. I shall leave you to work out what it is.

    In future use spaces not tabs for indenting, and use some whitespace. o+="g" is difficult to read whereas o += "g" is much easier.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic