Bug 57925 - NPE on org.apache.poi.ss.formula.OperationEvaluationContext#createExternSheetRefEvaluator(org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet)
Summary: NPE on org.apache.poi.ss.formula.OperationEvaluationContext#createExternSheet...
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: SS Common (show other bugs)
Version: 3.12-FINAL
Hardware: All All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-05-12 16:35 UTC by TimReset
Modified: 2015-09-19 20:16 UTC (History)
0 users



Attachments
NPE-example file (67.50 KB, application/vnd.ms-excel)
2015-05-12 16:35 UTC, TimReset
Details

Note You need to log in before you can comment on or make changes to this bug.
Description TimReset 2015-05-12 16:35:15 UTC
Created attachment 32733 [details]
NPE-example file

I tried parse xls file with incorrect formula (incorrect link to cell) and give NPE:
<pre>
Caused by: java.lang.NullPointerException
	at org.apache.poi.ss.formula.OperationEvaluationContext.createExternSheetRefEvaluator(OperationEvaluationContext.java:97)
	at org.apache.poi.ss.formula.OperationEvaluationContext.createExternSheetRefEvaluator(OperationEvaluationContext.java:88)
	at org.apache.poi.ss.formula.OperationEvaluationContext.getRef3DEval(OperationEvaluationContext.java:305)
	at org.apache.poi.ss.formula.WorkbookEvaluator.getEvalForPtg(WorkbookEvaluator.java:631)
	at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateNameFormula(WorkbookEvaluator.java:690)
	at org.apache.poi.ss.formula.WorkbookEvaluator.getEvalForNameRecord(WorkbookEvaluator.java:679)
	at org.apache.poi.ss.formula.WorkbookEvaluator.getEvalForPtg(WorkbookEvaluator.java:597)
	at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateFormula(WorkbookEvaluator.java:505)
	at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:263)
	at org.apache.poi.ss.formula.WorkbookEvaluator.evaluate(WorkbookEvaluator.java:205)
	at org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.evaluateFormulaCellValue(HSSFFormulaEvaluator.java:374)
	at org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.evaluateFormulaCell(HSSFFormulaEvaluator.java:234)
	at org.apache.poi.ss.usermodel.DataFormatter.formatCellValue(DataFormatter.java:816)
	at com.ptnl.acube.server.excel.WorkbookProcessor.getCellValue(WorkbookProcessor.java:233)
</pre>

It's mistake on code:
<pre>
SheetRangeEvaluator createExternSheetRefEvaluator(ExternalSheet externalSheet) {
		WorkbookEvaluator targetEvaluator;
		int otherFirstSheetIndex;
		int otherLastSheetIndex = -1;
		if (externalSheet == null || externalSheet.getWorkbookName() == null) {
			// sheet is in same workbook
			targetEvaluator = _bookEvaluator;
			otherFirstSheetIndex = _workbook.getSheetIndex(externalSheet.getSheetName()); //<-- row 97, mistake there
			
			if (externalSheet instanceof ExternalSheetRange) {
			    String lastSheetName = ((ExternalSheetRange)externalSheet).getLastSheetName();
			    otherLastSheetIndex = _workbook.getSheetIndex(lastSheetName);
			}
</pre>
If externalSheet == null then tried call method getSheetName() on externalSheet and give exception.
Comment 1 Dominik Stadler 2015-06-21 08:36:52 UTC
I tried to reproduce this but couldn't, can you post self-contained sample code which allows to see how you trigger this exception?
Comment 2 Dominik Stadler 2015-09-19 20:16:20 UTC
I found the flaw in my unit-test and have now adjusted the code to more gracefully handle such invalid external references in r1703999.