• 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

Wow, never quite realized this before...

 
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't a real test question, but it could easily be.

Assuming the following method is inside a class, and everything compiles nicely, what will happen when line #12 is executed?

(A) A java.lang.NullPointerException will definitely be thrown
(B) A java.lang.NullPointerException will definitely not be thrown
(C) Impossible to determine from the given code.


[code]
10. public void foo() {
11. MyTest test = null;
12. test.someMethod();
13. }
[code]

The answer is (C) because the method could be static. Wonder how many people would be caught by this.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice Observation. Pls do post these kind of questions in the SCJP forum, so that the exam takers will be benefitted.

Cheers
Arvind
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would be caught by it.

Fortunately, Eclipse can flag such code with a warning, and a quick fix to change the offending line to

MyTest.someMethod();

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic