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
This commit is contained in:
Dominik Stadler 2015-06-20 11:29:14 +00:00
parent ebf65abac7
commit cb5588c4a1
3 changed files with 17 additions and 2 deletions

View 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);
}

View 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 {

View 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));
}
}