Java Bug Hunt Medium +20 XP
NullPointerException
This code crashes with NPE.
JavaFind the bug
1
String s = null;2
if (s.equals("hello")) {3
System.out.println("Match");4
} The bug is on line 2
Select the correct fix
Show answer and explanation
Answer: Both A and B work
Calling .equals() on null throws NPE. Either null-check first (if (s != null && ...)) or use the literal first ("hello".equals(s)) which handles null safely.
Hint: A string literal can never be null.
What this challenge tests
Bug Hunt in Java: null, npe.