Bug 65227 - possible NPE in org.apache.poi.ss.util.SheetUtil.getCellWidth(cell,defaultCharWidth,formatter,useMergedCells,mergedRegions)
Summary: possible NPE in org.apache.poi.ss.util.SheetUtil.getCellWidth(cell,defaultCha...
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 5.0.0-FINAL
Hardware: PC Linux
: P2 major (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-04-09 03:58 UTC by Petr Blaha
Modified: 2021-04-27 06:12 UTC (History)
0 users



Attachments
Pathed SheetUtil.java (17.48 KB, text/x-java)
2021-04-09 03:58 UTC, Petr Blaha
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Petr Blaha 2021-04-09 03:58:55 UTC
Created attachment 37802 [details]
Pathed SheetUtil.java

Our customer reported NPE error when processing excel files and since then we are patching  SheetUtil class, for many years. I cannot provide Detailed error log, we cannot test it on production.

When going thru code in my opinion NPE can happen in XSSF/SXSSF and i think not in HSSF  Workbooks. 

Would like to upstream this two lines fix so we do not have to keep separate file for this:

if (rt != null && rt.getString() != null) {
...
}


------Original version----------------

        if (cellType == CellType.STRING) { //this line number s 161 in SheetUtil.java
            RichTextString rt = cell.getRichStringCellValue();
            String[] lines = rt.getString().split("\\n");
            for (String line : lines) {
                String txt = line + 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);
            }

        

--------Patched version---------------------

if (cellType == CellType.STRING) { //this line number s 161 in SheetUtil.java
            RichTextString rt = cell.getRichStringCellValue();
            if (rt != null && rt.getString() != null) {
                String[] lines = rt.getString().split("\\n");
                for (String line : lines) {
                    String txt = line + 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);
                }
            }
        }
Comment 1 PJ Fanning 2021-04-09 08:21:40 UTC
Thanks. Added change with r1888553
Comment 2 Dominik Stadler 2021-04-27 06:12:12 UTC
Seems this is fixed via the commit mentioned in the previous comment.