Fixed potential NullPointer.

This commit is contained in:
Marten Deinum
2014-08-28 08:42:39 +02:00
committed by Michael Minella
parent 4e58b855e6
commit 6c4f0414c3
2 changed files with 14 additions and 6 deletions

View File

@@ -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<String> values = new ArrayList<String>();
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()]);

View File

@@ -82,4 +82,9 @@ public class JxlUtilsTests {
}
@Test
public void extractingContent() {
Assert.assertTrue("[null] should give empty array", JxlUtils.extractContents(null).length == 0);
}
}