]> source.dussan.org Git - poi.git/commitdiff
Bug 56420: Fix possible NullPointerException when empty cell is included in Sumif...
authorDominik Stadler <centic@apache.org>
Sat, 20 Jun 2015 11:29:14 +0000 (11:29 +0000)
committerDominik Stadler <centic@apache.org>
Sat, 20 Jun 2015 11:29:14 +0000 (11:29 +0000)
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
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
src/testcases/org/apache/poi/ss/formula/functions/TestSumif.java

index ef381adb0295a505165434367d3a0ffbdc0f5b10..977647fd20669fada7ce71433b4ee96529905996 100644 (file)
@@ -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);
        }
index 3dd7b0f265392b6c66a9e3add1047c9b545ae21c..646fe8e5bb7ae1a11e6fb49f0cc7b1c769b6dc33 100644 (file)
@@ -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 {
index 6cee5eab7cfd910b6e347cd506e434a298ffcda2..c761d22f1ad7e37851652509fda682f94faf1036 100644 (file)
@@ -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));
+       }
 }