diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 4b2bbdb948..25363f00ed 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -429,8 +429,12 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss private void validateSheetIndex(int index) { int lastSheetIx = _sheets.size() - 1; if (index < 0 || index > lastSheetIx) { + String range = "(0.." + lastSheetIx + ")"; + if (lastSheetIx == -1) { + range = "(no sheets)"; + } throw new IllegalArgumentException("Sheet index (" - + index +") is out of range (0.." + lastSheetIx + ")"); + + index +") is out of range " + range); } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index ab23bbf36f..e6d33ebeb0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -1040,8 +1040,12 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable lastSheetIx) { + String range = "(0.." + lastSheetIx + ")"; + if (lastSheetIx == -1) { + range = "(no sheets)"; + } throw new IllegalArgumentException("Sheet index (" - + index +") is out of range (0.." + lastSheetIx + ")"); + + index +") is out of range " + range); } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java index 15181245bc..b2357f947e 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java @@ -45,6 +45,8 @@ public abstract class BaseTestWorkbook extends TestCase { fail("should have thrown exceptiuon due to invalid sheet index"); } catch (IllegalArgumentException e) { // expected during successful test + // no negative index in the range message + assertFalse(e.getMessage().contains("-1")); } Sheet sheet0 = wb.createSheet();