Skip to content

Commit

Permalink
Fixing Apache POI issue - Formula Cell evaluation nullpointer exception
Browse files Browse the repository at this point in the history
  • Loading branch information
wkk91193 committed Jun 15, 2018
1 parent 2a3aaec commit 650e1dd
Showing 1 changed file with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ public static Long readAsLong(int colIndex, Row row) {
FormulaEvaluator eval = row.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
if(eval!=null) {
CellValue val = eval.evaluate(c);
return ((Double) val.getNumberValue()).longValue();
CellValue val = null;
try {
val = eval.evaluate(c);
} catch (NullPointerException npe) {
return null;
}
return ((Double)val.getNumberValue()).longValue();
}
}
else if (c.getCellType()==Cell.CELL_TYPE_NUMERIC){
Expand All @@ -85,7 +90,13 @@ public static String readAsString(int colIndex, Row row) {
FormulaEvaluator eval = row.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
if (eval!=null) {
CellValue val = eval.evaluate(c);
CellValue val = null;
try {
val = eval.evaluate(c);
} catch(NullPointerException npe) {
return null;
}

String res = trimEmptyDecimalPortion(val.getStringValue());
if (res!=null) {
if (!res.equals("")) {
Expand Down Expand Up @@ -136,7 +147,12 @@ public static Boolean readAsBoolean(int colIndex, Row row) {
FormulaEvaluator eval = row.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
if(eval!=null) {
CellValue val = eval.evaluate(c);
CellValue val = null;
try {
val = eval.evaluate(c);
} catch (NullPointerException npe) {
return false;
}
return val.getBooleanValue();
}
return false;
Expand All @@ -158,8 +174,13 @@ public static Integer readAsInt(int colIndex, Row row) {
FormulaEvaluator eval = row.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
if(eval!=null) {
CellValue val = eval.evaluate(c);
return ((Double) val.getNumberValue()).intValue();
CellValue val = null;
try {
val = eval.evaluate(c);
} catch (NullPointerException npe) {
return null;
}
return ((Double)val.getNumberValue()).intValue();
}
return null;
}else if (c.getCellType()==Cell.CELL_TYPE_NUMERIC) {
Expand All @@ -176,7 +197,12 @@ public static Double readAsDouble(int colIndex, Row row) {
FormulaEvaluator eval = row.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
if (eval!=null) {
CellValue val = eval.evaluate(c);
CellValue val = null;
try {
val = eval.evaluate(c);
} catch (NullPointerException npe) {
return 0.0;
}
return val.getNumberValue();
}else {
return 0.0;
Expand Down

0 comments on commit 650e1dd

Please sign in to comment.