Bird
0
0

What will be the response body when a NullPointerException is thrown and handled by this @ControllerAdvice?

medium📝 component behavior Q5 of 15
Spring Boot - Exception Handling
What will be the response body when a NullPointerException is thrown and handled by this @ControllerAdvice?
@ControllerAdvice
public class GlobalHandler {
  @ExceptionHandler(NullPointerException.class)
  public ResponseEntity handleNull(NullPointerException ex) {
    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Null value error");
  }
}
A"Internal server error"
B"Null value error"
C"Resource not found"
D"Success"
Step-by-Step Solution
Solution:
  1. Step 1: Identify exception and handler response

    The handler returns a ResponseEntity with body "Null value error" for NullPointerException.
  2. Step 2: Confirm response body content

    The response body is exactly "Null value error" as per the method.
  3. Final Answer:

    "Null value error" -> Option B
  4. Quick Check:

    Response body matches handler return = "Null value error" [OK]
Quick Trick: Response body matches handler's return string [OK]
Common Mistakes:
  • Assuming generic error message
  • Confusing with other exception messages
  • Ignoring handler's body content

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes