Bug 67778 - null pointer Exception for Area3Dpxg type in ExternalReference in Formula Evaluation
Summary: null pointer Exception for Area3Dpxg type in ExternalReference in Formula Eva...
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 5.2.3-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-10-17 07:20 UTC by wuchu
Modified: 2024-02-25 12:53 UTC (History)
0 users



Attachments
the main Excle (10.28 KB, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
2023-10-17 07:20 UTC, wuchu
Details
ExternalRefence Excel (11.33 KB, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
2023-10-17 09:16 UTC, wuchu
Details

Note You need to log in before you can comment on or make changes to this bug.
Description wuchu 2023-10-17 07:20:02 UTC
Created attachment 39150 [details]
the main Excle

public static void main(String[] args) {

        ZipSecureFile.setMinInflateRatio(-1.0d);
       
        String path = "\\apache POI\\apachePOI\\src\\main\\resources\\test_template.xlsx";
        try{
            OutputStream os = new FileOutputStream(new File("\\apache POI\\apachePOI\\src\\main\\resources\\extRef\\test_template_2.xlsx"));
            Workbook workbook = new XSSFWorkbook(new FileInputStream(path));
            FormulaEvaluator mainWorkbookEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
            File file = new File("\\apache POI\\apachePOI\\src\\main\\resources\\extRef\\test1.xlsx");
            Map<String, FormulaEvaluator> formulaEvaluatorHashMap = new HashMap<String, FormulaEvaluator>();
            formulaEvaluatorHashMap.put("report1.xlsx", mainWorkbookEvaluator);
            formulaEvaluatorHashMap.put("test1.xlsx", WorkbookFactory.create(file).getCreationHelper().createFormulaEvaluator());
            mainWorkbookEvaluator.setupReferencedWorkbooks(formulaEvaluatorHashMap);
            mainWorkbookEvaluator.evaluateAll();
            workbook.setForceFormulaRecalculation(false);
            workbook.write(os);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }


java.lang.NullPointerException
	at org.apache.poi.xssf.usermodel.BaseXSSFFormulaEvaluator.cacheExternalWorkbookCells(BaseXSSFFormulaEvaluator.java:99)
	at org.apache.poi.xssf.usermodel.BaseXSSFFormulaEvaluator.evaluateFormulaCellValue(BaseXSSFFormulaEvaluator.java:62)
	at org.apache.poi.ss.formula.BaseFormulaEvaluator.evaluateFormulaCell(BaseFormulaEvaluator.java:184)
	at org.apache.poi.ss.formula.BaseFormulaEvaluator.evaluateAllFormulaCells(BaseFormulaEvaluator.java:277)
	at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluateAll(XSSFFormulaEvaluator.java:100)
	
in BaseFormulaEvaluator.java:96 ,XSSFWorkbook externalWorkbook =(XSSFWorkbook) xssfCell.getSheet().getWorkbook().getCreationHelper().getReferencedWorkbooks().get(externalSheet.getWorkbookName());
 getReferencedWorkbooks().size()=0,so externalWorkbook is null
Comment 1 PJ Fanning 2023-10-17 08:36:54 UTC
That external workbook code looks like it needs more mull checks. I added r1913045 as a preliminary change.
Comment 2 wuchu 2023-10-17 09:16:59 UTC
Created attachment 39152 [details]
ExternalRefence Excel
Comment 3 wuchu 2023-10-24 03:01:39 UTC
Finally,I add addExternalWorkbook function in XSSFWorkbook.java  to add ExternalWorkbook to XSSFCreationHelper.referencedWorkbooks  and add some codes:

   formulaEvaluatorHashMap.put("test1.xlsx", WorkbookFactory.create(file).getCreationHelper().createFormulaEvaluator());
((XSSFWorkbook)workbook).addExternalWorkbook("test1.xlsx",                                    WorkbookFactory.create(file).getCreationHelper().createFormulaEvaluator()));

  then that's OK.
  But in BaseXSSFFormulaEvaluator.cacheExternalWorkbookCells(),there not check the row is null or cell is null .and there will throw null Exception.
so there add if check: 
     int nullRowCont = 0;
                        for (int rowIndex = firstRow; rowIndex <= lastRow; rowIndex++) {
                            XSSFRow row = sheet.getRow(rowIndex);
                            int firstColumn = area3DPxg.getFirstColumn();
                            int lastColumn = area3DPxg.getLastColumn();

//                            System.out.println("+++++row+++:"+rowIndex+" **row**:"+row);
                            if(null!=row) {
                                for (int cellIndex = firstColumn; cellIndex <= lastColumn; cellIndex++) {
//                                    System.out.println("*****cellIndex*****:" + cellIndex);
                                    XSSFCell cell = row.getCell(cellIndex);

//                                    System.out.println("*****cellvalue*$$$$:" + cell);
                                    String cellValue = "";
                                    String cellR = "";
                                    if (null != cell) {
                                        cellValue = cell.getRawValue();
                                        cellR = new CellReference(cell).formatAsString(false);
                                        externalLinksTable.cacheData(sheet.getSheetName(), (long) rowIndex + 1, cellR, cellValue);
                                    }

                                }
                            }else {
                                nullRowCont++;
                            }
                            if(nullRowCont>20){
                                break;
                            }

                        }
Comment 4 PJ Fanning 2023-10-24 09:36:28 UTC
I added more null checks with r1913258
Comment 5 Dominik Stadler 2024-02-25 12:53:46 UTC
Seems some checks were added here, thus closing this for now. 

Please report new issues if there are still some missing in latest versions of Apache POI. At least version 5.2.5 should include the changes from here.