From 6c4f0414c3b765dd8478f7dea6557c13aaeb9586 Mon Sep 17 00:00:00 2001 From: Marten Deinum Date: Thu, 28 Aug 2014 08:42:39 +0200 Subject: [PATCH] Fixed potential NullPointer. --- .../batch/item/excel/jxl/JxlUtils.java | 15 +++++++++------ .../batch/item/excel/jxl/JxlUtilsTests.java | 5 +++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlUtils.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlUtils.java index e3c4cbc..984eff6 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlUtils.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlUtils.java @@ -18,6 +18,7 @@ package org.springframework.batch.item.excel.jxl; import jxl.Cell; import jxl.Workbook; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import java.util.ArrayList; @@ -54,7 +55,7 @@ public final class JxlUtils { * @return true/false */ public static boolean isEmpty(final Cell[] row) { - if (row == null || row.length == 0) { + if (ObjectUtils.isEmpty(row)) { return true; } for (final Cell cell : row) { @@ -83,11 +84,13 @@ public final class JxlUtils { */ public static String[] extractContents(final Cell[] row) { final List values = new ArrayList(); - for (final Cell cell : row) { - if (!isEmpty(cell)) { - values.add(cell.getColumn(), cell.getContents()); - } else { - values.add(cell.getColumn(), ""); + if (!ObjectUtils.isEmpty(row)) { + for (final Cell cell : row) { + if (!isEmpty(cell)) { + values.add(cell.getColumn(), cell.getContents()); + } else { + values.add(cell.getColumn(), ""); + } } } return values.toArray(new String[values.size()]); diff --git a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/jxl/JxlUtilsTests.java b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/jxl/JxlUtilsTests.java index fbef6ef..e83c300 100644 --- a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/jxl/JxlUtilsTests.java +++ b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/jxl/JxlUtilsTests.java @@ -82,4 +82,9 @@ public class JxlUtilsTests { } + @Test + public void extractingContent() { + Assert.assertTrue("[null] should give empty array", JxlUtils.extractContents(null).length == 0); + } + }