Skip to content

Commit b67524d

Browse files
committed
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
1 parent d3afa15 commit b67524d

3 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<body>
4141

4242
<release version="1.0" date="TBD" description="First release">
43+
<action issue="CSV-122" type="fix" dev="britter" due-to="Mike Lewis">NullPointerException when empty header string and and null string of ""</action>
4344
<action issue="CSV-117" type="update" dev="sebb">Validate format parameters in constructor</action>
4445
<action issue="CSV-121" type="add" dev="ggregory" due-to="Sebastian Hardt">IllegalArgumentException thrown when the header contains duplicate names when the column names are empty.</action>
4546
<action issue="CSV-120" type="add" dev="ggregory" due-to="Sergei Lebedev">CSVFormat#withHeader doesn't work with CSVPrinter</action>

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ private Map<String, Integer> initializeHeader() throws IOException {
381381
for (int i = 0; i < headerRecord.length; i++) {
382382
final String header = headerRecord[i];
383383
final boolean containsHeader = hdrMap.containsKey(header);
384-
final boolean emptyHeader = header.trim().isEmpty();
384+
final boolean emptyHeader = header == null || header.trim().isEmpty();
385385
if (containsHeader && (!emptyHeader || (emptyHeader && !this.format.getIgnoreEmptyHeaders()))) {
386386
throw new IllegalArgumentException("The header contains a duplicate name: \"" + header +
387387
"\" in " + Arrays.toString(headerRecord));

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,12 @@ public void testHeadersMissing() throws Exception {
661661
CSVFormat.DEFAULT.withHeader().withIgnoreEmptyHeaders(true).parse(in).iterator();
662662
}
663663

664+
@Test
665+
public void testHeaderMissingWithNull() throws Exception {
666+
final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz");
667+
CSVFormat.DEFAULT.withHeader().withNullString("").withIgnoreEmptyHeaders(true).parse(in).iterator();
668+
}
669+
664670
@Test
665671
public void testHeaderComment() throws Exception {
666672
final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");

0 commit comments

Comments
 (0)