• 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

NullPointerException

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


The output for the above code snippet is a NullPointerException.
But it was mentioned that the answer is null in the webpage where I found the question.
Which is the correct output?
 
Enthuware Software Support
Posts: 4810
52
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried to compile and run it?
 
Ranch Hand
Posts: 145
4
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gautham,

The correct output is NullPointerException.
Why you are getting NullPointerException ? First Understand the internal working of for-each loop. when you compile your code for-each loop code will converted into equivalent for loop code .

See , how you code changes after compilation



Observe the below two lines


At first line xx is reference variable for int array,But it is assigned with null. At Second line, you calling xx.length, means null.length gives you NullPointerException. Remember when you call methods or properties on a reference variable which is null, you will get NullPointerException.

Hope it helps !
 
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gautham Muralidharan wrote:

The output for the above code snippet is a NullPointerException.
But it was mentioned that the answer is null in the webpage where I found the question.
Which is the correct output?



Hi Gautham -

To prepare for this exam, write, compile and execute multiple code snippets; to understand Java topics.

The questions in this exam will try to ensure that you know your Java inside out. Only reading and trying to comprehend code won't help.

Good Luck to you.

With much respect,
Mala
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always QuoteYourSources
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic