• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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
 
knowledge is the difference between drudgery and strategic action -- tiny ad
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic