From b67524da7fd146634c7112b23e95d1d45c398b82 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Fri, 11 Jul 2014 18:42:48 +0000 Subject: [PATCH] CSV-122: NullPointerException when empty header string and and null string of "". Thanks to Mike Lewis. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1609768 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 1 + src/main/java/org/apache/commons/csv/CSVParser.java | 2 +- src/test/java/org/apache/commons/csv/CSVParserTest.java | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 0e74adda..26ad32f3 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -40,6 +40,7 @@ + NullPointerException when empty header string and and null string of "" Validate format parameters in constructor IllegalArgumentException thrown when the header contains duplicate names when the column names are empty. CSVFormat#withHeader doesn't work with CSVPrinter diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index b6867a40..b7ca3fa9 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -381,7 +381,7 @@ public final class CSVParser implements Iterable, Closeable { for (int i = 0; i < headerRecord.length; i++) { final String header = headerRecord[i]; final boolean containsHeader = hdrMap.containsKey(header); - final boolean emptyHeader = header.trim().isEmpty(); + final boolean emptyHeader = header == null || header.trim().isEmpty(); if (containsHeader && (!emptyHeader || (emptyHeader && !this.format.getIgnoreEmptyHeaders()))) { throw new IllegalArgumentException("The header contains a duplicate name: \"" + header + "\" in " + Arrays.toString(headerRecord)); diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index 75531b5d..f5b6cb86 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -661,6 +661,12 @@ public class CSVParserTest { CSVFormat.DEFAULT.withHeader().withIgnoreEmptyHeaders(true).parse(in).iterator(); } + @Test + public void testHeaderMissingWithNull() throws Exception { + final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz"); + CSVFormat.DEFAULT.withHeader().withNullString("").withIgnoreEmptyHeaders(true).parse(in).iterator(); + } + @Test public void testHeaderComment() throws Exception { final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");