From d8edfc6e370f0ba54ae13a7c78d6486e465a8dde Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sat, 20 Jun 2015 11:29:14 +0000 Subject: [PATCH] Bug 56420: Fix possible NullPointerException when empty cell is included in Sumif calculation git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1686564 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/ss/formula/functions/Sumif.java | 6 ++++++ .../org/apache/poi/xssf/usermodel/TestXSSFBugs.java | 4 ++-- .../org/apache/poi/ss/formula/functions/TestSumif.java | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/poi/ss/formula/functions/Sumif.java b/src/java/org/apache/poi/ss/formula/functions/Sumif.java index ef381adb02..977647fd20 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Sumif.java +++ b/src/java/org/apache/poi/ss/formula/functions/Sumif.java @@ -69,6 +69,12 @@ public final class Sumif extends Var2or3ArgFunction { AreaEval aeSum) { // TODO - junit to prove last arg must be srcColumnIndex and not srcRowIndex I_MatchPredicate mp = Countif.createCriteriaPredicate(arg1, srcRowIndex, srcColumnIndex); + + // handle empty cells + if(mp == null) { + return NumberEval.ZERO; + } + double result = sumMatchingCells(aeRange, mp, aeSum); return new NumberEval(result); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 3dd7b0f265..646fe8e5bb 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -1538,7 +1538,6 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { * SUMIF was throwing a NPE on some formulas */ @Test - @Ignore("This bug is still to be fixed") public void testBug56420SumIfNPE() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56420.xlsx"); @@ -1548,7 +1547,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { Row r = sheet.getRow(2); Cell c = r.getCell(2); assertEquals("SUMIF($A$1:$A$4,A3,$B$1:$B$4)", c.getCellFormula()); - evaluator.evaluateInCell(c); + Cell eval = evaluator.evaluateInCell(c); + assertEquals(0.0, eval.getNumericCellValue(), 0.0001); } private void bug53798Work(Workbook wb, File xlsOutput) throws IOException { diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestSumif.java b/src/testcases/org/apache/poi/ss/formula/functions/TestSumif.java index 6cee5eab7c..c761d22f1a 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestSumif.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestSumif.java @@ -21,6 +21,8 @@ import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.apache.poi.ss.formula.eval.AreaEval; +import org.apache.poi.ss.formula.eval.BlankEval; +import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.NumericValueEval; import org.apache.poi.ss.formula.eval.StringEval; @@ -107,4 +109,11 @@ public final class TestSumif extends TestCase { confirmDouble(60, ve); } + + public void testEvaluateException() { + assertEquals(ErrorEval.VALUE_INVALID, invokeSumif(-1, -1, BlankEval.instance, new NumberEval(30.0))); + assertEquals(ErrorEval.VALUE_INVALID, invokeSumif(-1, -1, BlankEval.instance, new NumberEval(30.0), new NumberEval(30.0))); + assertEquals(ErrorEval.VALUE_INVALID, invokeSumif(-1, -1, new NumberEval(30.0), BlankEval.instance, new NumberEval(30.0))); + assertEquals(ErrorEval.VALUE_INVALID, invokeSumif(-1, -1, new NumberEval(30.0), new NumberEval(30.0), BlankEval.instance)); + } } -- 2.39.5