• 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

Another question for NPE.........

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following class will never throw NullPointerException.

Answer is true.
Can any one explain me why does it NOT throw a NullPointerException??
Sonir
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sonir,
this is because of the exception catching and throwing mechanism.
[] has higher priority than =. so ml() will be first called to evaluate the index of the array (dont care yet that whther array has been allocated memory or not).
ml() throws exception which is forwarded to the calling point that is in main() method. and we have main() method not catching that exception but declares to "throw" an exception.
thus it throws the exception further which JVM has to handle now and so JVM exists after handling the exception and u never reach at the point when the array element evaluated by the m1l() call gets resolved and u try to access the member a[ml()].
so, u don't get NullPointerException.
regards
maulin
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic