Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

please explain this runtime exception..

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
pl look at this code:

the code gives NullPointerException on lines 8 and 17
whether line 7 is commented or not
when the line is commented, what is the class of nI? Object,I guess. then why is it not displaying the class in line 8 and throwing an exception instead?
when line 7 is not commented, the test in line 9 fails ,and "no" should be printed..
could somebody explain please?
thanks in advance
Ramani.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<The code gives NullPointerException on lines 8 and 17 <br /> whether line 7 is commented or not when the line is commented, what is the class of nI? Object,I guess. then why is it not displaying the class in line 8 and throwing an exception instead?>
Whether line 7 is commented or not the class level variables are initialised to null,in your program commenting or uncommenting line 7 has absolutely no effect and in line 8 you are calling the method getClass on null object and at this point of time the program will throw an exception and it won't even go into the if..else part,comment line 8 and then the program will print no.
Surya
 
ramani2020
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup! I couldnt be more dumb
I guess I shd start from scrap 'gain...
thanks Surya..
Ramani.
 
reply
    Bookmark Topic Watch Topic
  • New Topic