When a NullPointerException occurs after a button click, the goal is to identify which object is null and why it was not initialized before use. The most effective approach is to analyze the stack trace, trace the execution flow triggered by the button event, and verify that all required objects, dependencies, and UI components are properly initialized before they are accessed.
Suppose a "Submit" button calls userService.save(user). If userService was never initialized, clicking the button will throw a NullPointerException. Reviewing the stack trace and checking object initialization quickly reveals the root cause.
public class UserController {
private UserService userService;
public void onSubmit() {
if (userService == null) {
System.out.println(
"UserService is not initialized");
return;
}
userService.save();
}
}
class UserService {
public void save() {
System.out.println(
"User Saved");
}
}A concise interview answer is: I would first examine the stack trace to identify the exact line causing the NullPointerException. Then I would trace the button-click flow, verify object initialization, add appropriate logging and null checks, and fix the root cause rather than simply suppressing the exception.
Last updated: 11 November 2026
Previous question: How would you implement a deep copy in Java?
Next question: What are the disadvantages of JIT compilation and in what scenarios might you consider disabling JIT compilation?