Search code examples
javajopendocument

IndexOutOfBoundException when trying trying to get a .ods cell with jOpenDocument


I am facing a little challenge with my coding.

File file = new File("template.ods");
Sheet sheet;
try {
    // load file
    sheet = SpreadSheet.createFromFile(file).getSheet("Certificate");
    System.out.println(file);
    System.out.println(sheet.getCellAt("A1").isEmpty());
    sheet.setValueAt("A1", 1, 1);;
    System.out.println(sheet.getCellAt(1, 1).getTextValue());
    sheet.getCellAt(2, 2).setValue("B2");
    sheet.getCellAt(3, 3).setValue("C3");
    sheet.getCellAt(4, 4).setValue("D4");
    // Save to file and open it.
    File outputFile = new File("fillingTest.ods");
    OOUtils.open(sheet.getSpreadSheet().saveAs(outputFile));

} catch (Exception e) {
    e.printStackTrace();
}

I am getting to know the jOpenDocument-Library. I want to fill an existing OpenOffice-Spreadsheet-Template (template.ods) with some sample values. When running the above code, the console shows this:

template.ods
true

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at org.jopendocument.dom.spreadsheet.Table.getRow(Unknown Source)
    at org.jopendocument.dom.spreadsheet.Table.getImmutableCellAt(Unknown Source)
    at org.jopendocument.dom.spreadsheet.Table.getValueAt(Unknown Source)
    at org.jopendocument.dom.spreadsheet.Table.setValueAt(Unknown Source)
    at jOpenDocument.createDocument.main(createDocument.java:48)

"template.ods" and "true" shows that the application retrieved the file from directory and "true" that it could read the cell, which is empty.

But I don't know which Array throws the Exception and why it says "Unknown Source".


Solution

  • Array of rows throws the exception. Try adding more rows with sheet.setRowCount() before setting cell contents.