Menu

#96 TextAreaFigure throwing StringIndexOutOfBoundsException

open
nobody
None
5
2006-02-16
2006-02-16
No

From the method getNextParagraph(String text, Point pos) :

int end = text.indexOf('\n', start);
if (end == -1) {
end = text.length();
}
pos.y = end;
// check for "\r\n" sequence
if (text.charAt(end - 1) == '\r') {
return text.substring(start, end - 1);
}
else {
return text.substring(start, end);
}

If text begins with a "\n" character, end is set to 0.
The method call 'text.charAt(end - 1)' will then throw
a StringIndexOutOfBoundsException.

Quick Fix:

if (end != 0 && text.charAt(end - 1) == '\r') {
return text.substring(start, end - 1);
}

Discussion


Log in to post a comment.

MongoDB Logo MongoDB