• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enhanced for question

 
James Qu
Greenhorn
Posts: 6
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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!
 
Henry Wong
author
Marshal
Pie
Posts: 22096
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
     
    Liutauras Vilda
    Bartender
    Pie
    Posts: 2783
    112
    BSD VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • 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
    Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You guys are awesome! Appreciate!
     
    Campbell Ritchie
    Sheriff
    Posts: 51366
    87
    • Mark post as helpful
    • send pies
    • 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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic