Skip to content
This repository was archived by the owner on Sep 14, 2025. It is now read-only.

Commit d8d5de6

Browse files
author
Gary Gregory
committed
[CSV-239] Cannot get headers in column order from CSVRecord.
Some NPE-proofing.
1 parent 8a5efde commit d8d5de6

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ private Map<String, Integer> createHeaderMap() throws IOException {
466466
if (formatHeader != null) {
467467
hdrMap = this.format.getIgnoreHeaderCase() ?
468468
new TreeMap<>(String.CASE_INSENSITIVE_ORDER) :
469-
new LinkedHashMap<>();
469+
new TreeMap<>();
470470

471471
String[] headerRecord = null;
472472
if (formatHeader.length == 0) {
@@ -486,13 +486,15 @@ private Map<String, Integer> createHeaderMap() throws IOException {
486486
if (headerRecord != null) {
487487
for (int i = 0; i < headerRecord.length; i++) {
488488
final String header = headerRecord[i];
489-
final boolean containsHeader = hdrMap.containsKey(header);
489+
final boolean containsHeader = header == null ? false : hdrMap.containsKey(header);
490490
final boolean emptyHeader = header == null || header.trim().isEmpty();
491491
if (containsHeader && (!emptyHeader || !this.format.getAllowMissingColumnNames())) {
492-
throw new IllegalArgumentException("The header contains a duplicate name: \"" + header +
493-
"\" in " + Arrays.toString(headerRecord));
492+
throw new IllegalArgumentException("The header contains a duplicate name: \"" + header
493+
+ "\" in " + Arrays.toString(headerRecord));
494+
}
495+
if (header != null) {
496+
hdrMap.put(header, Integer.valueOf(i));
494497
}
495-
hdrMap.put(header, Integer.valueOf(i));
496498
}
497499
}
498500
}

0 commit comments

Comments
 (0)