Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wow, never quite realized this before...

 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • 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.
 
Arvind Sampath
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • 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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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();

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic