Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/org/apache/commons/csv/CSVRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public String get(final String name) {
if (headerMap == null) {
throw new IllegalStateException("No header mapping was specified, the record values can't be accessed by name");
}
final Integer index = headerMap.get(name);
final Integer index = name == null ? null : headerMap.get(name);
if (index == null) {
throw new IllegalArgumentException(String.format("Mapping for %s not found, expected one of %s", name, headerMap.keySet()));
}
Expand Down Expand Up @@ -243,7 +243,7 @@ public boolean isConsistent() {
*/
public boolean isMapped(final String name) {
final Map<String, Integer> headerMap = getHeaderMapRaw();
return headerMap != null && headerMap.containsKey(name);
return name != null && headerMap != null && headerMap.containsKey(name);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/apache/commons/csv/CSVRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class CSVRecordTest {

Expand Down Expand Up @@ -228,6 +230,22 @@ void testIsSetString() {
assertFalse(recordWithHeader.isSet("DOES NOT EXIST"));
}

@ParameterizedTest
@ValueSource(booleans = { false, true })
void testNullNameAccessorsMatchAcrossIgnoreHeaderCase(final boolean ignoreHeaderCase) throws IOException {
final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().setSkipHeaderRecord(true).setIgnoreHeaderCase(ignoreHeaderCase).get();
try (CSVParser parser = CSVParser.parse("A,B\n1,2", format)) {
final CSVRecord rec = parser.iterator().next();
// A null name is never a mapped header, so the boolean guards return false rather than throwing,
// regardless of ignoreHeaderCase (the case-insensitive header map rejects null keys).
assertFalse(rec.isMapped(null));
assertFalse(rec.isSet((String) null));
// A null name (also reached from get((Enum) null)) reports a missing mapping, not an NPE.
assertThrows(IllegalArgumentException.class, () -> rec.get((String) null));
assertThrows(IllegalArgumentException.class, () -> rec.get((Enum<?>) null));
}
}

@Test
void testIterator() {
int i = 0;
Expand Down
Loading