Skip to content

Commit

Permalink
Closed #386
Browse files Browse the repository at this point in the history
Fixed `NullPointerException` in `PoiExcelUtil.getTableDataAsMap` when
last column is empty

Code enhancement to provide empty or blank cell with null value.

Example data is as below:

Before this update:

EXLS

{Id=1.0, firstname=Test1, lastname=L1, activated=true, created=Mon Mar
01 00:00:00 PST 2021, version=1.1, [email protected], index=1.0}
{Id=2.0, firstname=Test2, lastname=L2, activated=false, created=Tue Mar
02 00:00:00 PST 2021, version=1.2, [email protected], index=2.0}
{Id=null, firstname=Test3, lastname=L3, activated=true, created=Wed Mar
03 00:00:00 PST 2021, version=1.3, [email protected]}
{Id=4.0, firstname=Test4, lastname=L4, activated=false, created=Thu Mar
04 00:00:00 PST 2021, version=1.4, [email protected], index=4.0}

XLS
{Id=1.0, firstname=Test1, lastname=L1, activated=true, created=Mon Mar
01 00:00:00 PST 2021, version=1.1, [email protected], index=1.0}
{Id=2.0, firstname=Test2, lastname=L2, activated=false, created=Tue Mar
02 00:00:00 PST 2021, version=1.2, [email protected], index=2.0}
{Id=, firstname=Test3, lastname=L3, activated=true, created=Wed Mar 03
00:00:00 PST 2021, version=1.3, [email protected]}
{Id=4.0, firstname=Test4, lastname=L4, activated=false, created=Thu Mar
04 00:00:00 PST 2021, version=1.4, [email protected], index=4.0}


After this update:

XLSX/XLS

{Id=1.0, firstname=Test1, lastname=L1, activated=true, created=Mon Mar
01 00:00:00 PST 2021, version=1.1, [email protected], index=1.0}
{Id=2.0, firstname=Test2, lastname=L2, activated=false, created=Tue Mar
02 00:00:00 PST 2021, version=1.2, [email protected], index=2.0}
{Id=null, firstname=Test3, lastname=L3, activated=true, created=Wed Mar
03 00:00:00 PST 2021, version=1.3, [email protected], index=null}
{Id=4.0, firstname=Test4, lastname=L4, activated=false, created=Thu Mar
04 00:00:00 PST 2021, version=1.4, [email protected], index=4.0}
  • Loading branch information
cjayswal committed Dec 30, 2021
1 parent bb15532 commit edf75de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/com/qmetry/qaf/automation/util/ExcelUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,17 @@ public static Object[][] getExcelDataAsMap(String file, String sheetName) {
}
} else {
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (int col = firstCol; col < (firstCol + cells.length); col++) {
map.put(colNames[col - firstCol], StringUtil.toObject(cells[col].getContents()));
for (int col = firstCol; col < (firstCol + colNames.length); col++) {
if(cells.length>col) {
String coldata = cells[col].getContents();
if(StringUtil.isNullOrEmpty(coldata)) {
map.put(colNames[col - firstCol], null);
}else {
map.put(colNames[col - firstCol], StringUtil.toObject(coldata));
}
}else {
map.put(colNames[col - firstCol], null);
}
}
retobj[row - (firstRow + 1)][0] = map;
}
Expand Down Expand Up @@ -287,11 +296,15 @@ public static Object[][] getTableDataAsMap(String xlFilePath, String tableName,
} else {
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (int j = startCol + 1; j < endCol; j++, cj++) {
map.put(colNames[cj], StringUtil.toObject(sheet.getCell(j, i).getContents()));
String coldata = sheet.getCell(j, i).getContents();
if(StringUtil.isNullOrEmpty(coldata)) {
map.put(colNames[cj], null);
}else {
map.put(colNames[cj], StringUtil.toObject(coldata));
}
}
logger.debug("Record " + ci + ":" + map);
tabArray[ci++][0] = map;

}
}
} catch (Exception e) {
Expand Down
5 changes: 4 additions & 1 deletion src/com/qmetry/qaf/automation/util/PoiExcelUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static Object[][] getExcelDataAsMap(String file, String sheetName) {
}
} else {
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (int col = firstCol; col < row.getLastCellNum(); col++) {
for (int col = firstCol; col < firstCol+colNames.length; col++) {
map.put(colNames[col - firstCol], getCellContent(row.getCell(col)));
}
retobj[rIndex - (firstRow + 1)][0] = map;
Expand Down Expand Up @@ -264,6 +264,9 @@ public static Object getCellContent(Cell cell) {
if (cell != null) {
FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
CellValue cellValue = evaluator.evaluate(cell);
if (cellValue == null) {
return null;
}
switch (cellValue.getCellType()) {
case NUMERIC:
if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
Expand Down

0 comments on commit edf75de

Please sign in to comment.