Search code examples
javaapache-poivaadinvaadin7

What are the scenarios when getFontIndex() API will return negative index?


I am using vaadin spreadsheet. In my program I am reading metadata stored in db like isBold, isItalic, etc and applying these italic , bold etc styles on cell depending on metadata . After I apply these styles I have to refresh the cell using spreadsheet.getSpreadsheetStyleFactory().cellStyleUpdated(cell, true); (Vaadin spreadsheet api). This api sometimes fails throwing ArrayIndexOutOFBoundsException which can be found below

Caused by: java.lang.ArrayIndexOutOfBoundsException: -32768 at java.util.ArrayList.elementData(ArrayList.java:400) [rt.jar:1.7.0_80] at java.util.ArrayList.get(ArrayList.java:413) [rt.jar:1.7.0_80] at org.apache.poi.xssf.model.StylesTable.getFontAt(StylesTable.java:386) [seqnc-common-jar-1.0.0.jar:] at org.apache.poi.xssf.usermodel.XSSFCellStyle.getFont(XSSFCellStyle.java:557) [seqnc-common-jar-1.0.0.jar:] at com.vaadin.addon.spreadsheet.XSSFColorConverter.colorStyles(XSSFColorConverter.java:97) [seqnc-common-jar-1.0.0.jar:] at com.vaadin.addon.spreadsheet.SpreadsheetStyleFactory.addCellStyleCSS(SpreadsheetStyleFactory.java:594) [seqnc-common-jar-1.0.0.jar:] at com.vaadin.addon.spreadsheet.SpreadsheetStyleFactory.cellStyleUpdated(SpreadsheetStyleFactory.java:461) [seqnc-common-jar-1.0.0.jar:]

What is happening here is XssfCellStyle is somehow having negative index -32768 which is being used as index in ArrayList.

I Just want to know under what scenarios fontIndex will be set to negative value in XssfCellStyle


Solution

  • Found the problem. My code was unnecessarily creating lots of (Around 40,0000 )Font objects.So 40K + fontindexs were needed to store them. But when I saw source of XSSFCellStle.getFontIndex() method integer index was type cast to short

     @Override
    public short getFontIndex() {
        return (short) getFontId();
    }
    

    Because I had more than 40K objects, index value was exceeding limit for Short datatype which is 32767. So any index above 32767 was coming as negative short value. I fixed problem by limiting my Font objects to below 32767 range