There are never magical meanings in the code, it just hurts you. Try:
int messagesToDisplay = 100; folder.getMessages(totalMessages - messagesToDisplay , totalMessages);
From JavaDoc, messages are identified using array 1, not 0.
Can you also add a stack trace.
source share