Skip to content
Advertisement

java.lang.IllegalArgumentException: fromIndex(x) > toIndex(y)

I convert list to Page for display table of candidates with pagination, but I am getting error

java.lang.IllegalArgumentException: fromIndex(5) > toIndex(1)

How to solve this issue?

JavaScript

Advertisement

Answer

This exception happens because when computing start of your slice you don’t take into consideration how many objects are there in full list of candidates.

If I request from your method a page 1 of size 5, but you only have 2 candidates, look what happens:

JavaScript

You have to especially consider the case when someone requested from you the page that does not exist. In that case, you have to either return an empty page, or a null, or something else, but important part is it has to be processed on a different code path.

So, here’s some code that should almost work: you have to implement the nonExistentPage method yourself.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement