[CSV-239] Cannot get headers in column order from CSVRecord.

Some NPE-proofing.
This commit is contained in:
Gary Gregory 2019-05-19 09:32:46 -04:00
parent 8a5efdeb7e
commit d8d5de6476
1 changed files with 7 additions and 5 deletions

View File

@ -466,7 +466,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
if (formatHeader != null) {
hdrMap = this.format.getIgnoreHeaderCase() ?
new TreeMap<>(String.CASE_INSENSITIVE_ORDER) :
new LinkedHashMap<>();
new TreeMap<>();
String[] headerRecord = null;
if (formatHeader.length == 0) {
@ -486,13 +486,15 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
if (headerRecord != null) {
for (int i = 0; i < headerRecord.length; i++) {
final String header = headerRecord[i];
final boolean containsHeader = hdrMap.containsKey(header);
final boolean containsHeader = header == null ? false : hdrMap.containsKey(header);
final boolean emptyHeader = header == null || header.trim().isEmpty();
if (containsHeader && (!emptyHeader || !this.format.getAllowMissingColumnNames())) {
throw new IllegalArgumentException("The header contains a duplicate name: \"" + header +
"\" in " + Arrays.toString(headerRecord));
throw new IllegalArgumentException("The header contains a duplicate name: \"" + header
+ "\" in " + Arrays.toString(headerRecord));
}
if (header != null) {
hdrMap.put(header, Integer.valueOf(i));
}
hdrMap.put(header, Integer.valueOf(i));
}
}
}