Bug 57034 - NullPointerException in autoSizeColumn
Summary: NullPointerException in autoSizeColumn
Alias: None
Product: POI
Classification: Unclassified
Component: SXSSF (show other bugs)
Version: 3.11-dev
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2014-09-29 02:20 UTC by guoh0115
Modified: 2016-02-11 15:14 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description guoh0115 2014-09-29 02:20:12 UTC
Workbook workbook = new SXSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        Row row = sheet.createRow(1);
        Cell cell = row.createCell(0);
        String x = null;
        sheet.autoSizeColumn(0, true);

I tested HSSFWorkbook, XSSFWorkbook, SXSSFWorkbook, only SXSSFWorkbook throws NullPointerException

due to different result to null String value

cell.getRichStringCellValue().getString() return null
Comment 1 Nick Burch 2014-09-29 11:19:03 UTC
I'm not sure that setting a null String as the cell value should be allowed, so I think we should probably be throwing an exception at the cell.setCellValue((String)null) point, rather than later in the auto size

Can anyone spot a problem with that as an approach?
Comment 2 Worsik 2016-02-09 08:14:03 UTC
Still valid on 3.11-Final

I think that NPE should be prevented when autosizing column and value should be treated as empty string.

Code of SheetUtil.java

        if (cellType == Cell.CELL_TYPE_STRING) {
            RichTextString rt = cell.getRichStringCellValue();
!!!         String[] lines = rt.getString().split("\\n");      !!! this is wrong !!!
            for (int i = 0; i < lines.length; i++) {
                String txt = lines[i] + defaultChar;

                AttributedString str = new AttributedString(txt);
                copyAttributes(font, str, 0, txt.length());

                if (rt.numFormattingRuns() > 0) {
                    // TODO: support rich text fragments

                width = getCellWidth(defaultCharWidth, colspan, style, width, str);

It could be a benefit if we can set how to treat NULL values when setting Cell values. For me, it would be better to accept NULL value as empty string (maybe based on some parameter). When exporting list of objects to Excel via POI, it is quite annoying to handle all getters like 

        object.getValue() == null ? "" : object.getValue();
Comment 3 Nick Burch 2016-02-11 15:14:31 UTC
In r1729849 I've added a common unit test for HSSF + XSSF + SXSSF for this. It already passed with no changes needed for the first two, and is now fixed for SXSSF. Setting a string cell value of null then auto-sizing works fine, with the column getting the default size if no other values exist