๐Ÿ”ด The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOG2026-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)

Solution 1โœ“ Most common cause

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); } }
Solution 2

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); }
Solution 3

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

Spring Boot 2.x

Uses PageRequest.of().

Spring Boot 3.x

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.

Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase