Summary: | NullPointerException in autoSizeColumn | ||
---|---|---|---|
Product: | POI | Reporter: | guoh0115 |
Component: | SXSSF | Assignee: | POI Developers List <dev> |
Status: | RESOLVED FIXED | ||
Severity: | normal | ||
Priority: | P2 | ||
Version: | 3.11-dev | ||
Target Milestone: | --- | ||
Hardware: | PC | ||
OS: | All |
Description
guoh0115
2014-09-29 02:20:12 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? 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(); 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 |