๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
2026-03-01 21:05:18.200 ERROR 8842 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.InvalidDataAccessApiUsageException: Pageable must not be null!] with root cause
org.springframework.dao.InvalidDataAccessApiUsageException: Pageable must not be null!โก Quick Fix Works 80% of the time
Create a PageRequest object and pass it to the repository, or make the Pageable parameter optional.
Pageable pageable = PageRequest.of(0, 10);
Page<User> users = userRepository.findAll(pageable);๐ง Why this Happens
Tap to expand the deep technical explanation
Your Spring Data JPA repository method signature includes a Pageable parameter. When the method was called from the service layer, 'null' was passed instead of a valid Pageable object. Spring Data strictly enforces this to prevent N+1 query issues on large datasets.
The HITEC City Parking Spot Analogy:
Imagine asking a librarian for a specific page of a book, but you don't tell them which page. The librarian (Spring Data) refuses to read the entire book aloud and demands you specify a page number (Pageable).
๐ How to Reproduce Confirm this is your error
Pass a null Pageable into a Spring Data repository method (for example findAll(null)), then call it. Spring Data throws InvalidDataAccessApiUsageException: Pageable must not be null.
๐ ๏ธ Solutions (3 Ways to Fix)
Pass a PageRequest object
๐ Use this when calling repository methods that accept Pageable.
Spring Data JPA requires an actual Pageable instance. You cannot pass null. Use PageRequest.of(page, size) to create one.
@Service
public class UserService {
public Page<User> getUsers(int page, int size) {
Pageable pageable = PageRequest.of(page, size);
return userRepository.findAll(pageable);
}
}Use @PageableDefault in Controller
๐ Use this if you want Spring to auto-create the Pageable from query params.
Spring can automatically map ?page=0&size=10 to a Pageable object if you use @PageableDefault.
@GetMapping("/users")
public Page<User> getUsers(@PageableDefault(size = 20, page = 0) Pageable pageable) {
return userRepository.findAll(pageable);
}Make Pageable optional
๐ Use this if you want to support both paginated and non-paginated responses.
Wrap the Pageable in an Optional or use required = false.
@GetMapping("/users")
public Page<User> getUsers(@RequestParam(required = false) Integer page) {
Pageable pageable = (page == null) ? Pageable.unpaged() : PageRequest.of(page, 10);
return userRepository.findAll(pageable);
}๐ Version Notes
Uses PageRequest.of().
Uses PageRequest.of(), but also supports OffsetBasedPageRequest.
๐ก๏ธ How to Prevent This Next Time
Always validate pagination inputs. Use @PageableDefault in controllers to guarantee a valid object is passed.