XSSFWorkbook#createCellStyle() fails with ArrayIndexOutOfBoundsException

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

XSSFWorkbook#createCellStyle() fails with ArrayIndexOutOfBoundsException

nestoru
Hi,

I am using the below code (see the commented portion of it for error
details) which is working fine for HSSF but fails for XSSF. The
original code can be reached from
http://www.coderanch.com/t/420958/open-source/Copying-sheet-excel-file-another
(for HSSF). I ended up ignoring the style for now and I am able to
merge my workbooks as explained in the coderanch post however styles
are important of course.

Thank you in advance,
-Nestor

public static void copyCell(XSSFCell oldCell, XSSFCell newCell,
Map<Integer, XSSFCellStyle> styleMap) {
        if (styleMap != null) {
            if (oldCell.getSheet().getWorkbook() ==
newCell.getSheet().getWorkbook()) {
                newCell.setCellStyle(oldCell.getCellStyle());
            } else {
                int stHashCode = oldCell.getCellStyle().hashCode();
                XSSFCellStyle newCellStyle = styleMap.get(stHashCode);
                if (newCellStyle == null) {
                    //The below produces:
                    /*
                    java.lang.ArrayIndexOutOfBoundsException: -1
                    at java.util.ArrayList.get(ArrayList.java:324)
                    at
org.apache.poi.xssf.model.StylesTable.getCellStyleXfAt(StylesTable.java:305)
                    at
org.apache.poi.xssf.usermodel.XSSFCellStyle.<init>(XSSFCellStyle.java:79)
                    at
org.apache.poi.xssf.model.StylesTable.createCellStyle(StylesTable.java:521)
                    at
org.apache.poi.xssf.usermodel.XSSFWorkbook.createCellStyle(XSSFWorkbook.java:443)
                    */
                    newCellStyle =
newCell.getSheet().getWorkbook().createCellStyle();
                    newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
                    styleMap.put(stHashCode, newCellStyle);
                }
                newCell.setCellStyle(newCellStyle);
            }
        }
        switch (oldCell.getCellType()) {
            case XSSFCell.CELL_TYPE_STRING:
                newCell.setCellValue(oldCell.getStringCellValue());
                break;
            case XSSFCell.CELL_TYPE_NUMERIC:
                newCell.setCellValue(oldCell.getNumericCellValue());
                break;
            case XSSFCell.CELL_TYPE_BLANK:
                newCell.setCellType(XSSFCell.CELL_TYPE_BLANK);
                break;
            case XSSFCell.CELL_TYPE_BOOLEAN:
                newCell.setCellValue(oldCell.getBooleanCellValue());
                break;
            case XSSFCell.CELL_TYPE_ERROR:
                newCell.setCellErrorValue(oldCell.getErrorCellValue());
                break;
            case XSSFCell.CELL_TYPE_FORMULA:
                newCell.setCellFormula(oldCell.getCellFormula());
                break;
            default:
                break;
        }

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: XSSFWorkbook#createCellStyle() fails with ArrayIndexOutOfBoundsException

nestoru
Is this a bug? Should I opoen a ticket for it?

Thanks!
-Nestor
Reply | Threaded
Open this post in threaded view
|

Re: XSSFWorkbook#createCellStyle() fails with ArrayIndexOutOfBoundsException

Nick Burch-11
On Tue, 13 Dec 2011, nestoru wrote:
> Is this a bug? Should I opoen a ticket for it?

It does seem there's something about your styles table that POI isn't
expecting. Whether that's a POI bug, or a corrupt file I can't say from
the stacktrace

You'd need to open a new bug in bugzilla, upload the problematic file, and
if possible include a small junit unit test that triggers the problem. If
you can, then compare the styles entry for a style you can clone and one
you can't, and see if you can spot what's going wrong

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: XSSFWorkbook#createCellStyle() fails with ArrayIndexOutOfBoundsException

nestoru
Done! https://issues.apache.org/bugzilla/show_bug.cgi?id=52348

Thanks for your suggestion,

-Nestor