diff --git a/src/main/java/org/apache/commons/csv/CSVRecord.java b/src/main/java/org/apache/commons/csv/CSVRecord.java index 079dde66..ae7762d8 100644 --- a/src/main/java/org/apache/commons/csv/CSVRecord.java +++ b/src/main/java/org/apache/commons/csv/CSVRecord.java @@ -178,7 +178,10 @@ public final class CSVRecord implements Serializable, Iterable { */ > M putIn(final M map) { for (final Entry entry : mapping.entrySet()) { - map.put(entry.getKey(), values[entry.getValue().intValue()]); + final int col = entry.getValue().intValue(); + if (col < values.length) { + map.put(entry.getKey(), values[col]); + } } return map; } diff --git a/src/test/java/org/apache/commons/csv/CSVRecordTest.java b/src/test/java/org/apache/commons/csv/CSVRecordTest.java index 9de28ee2..0e40ae44 100644 --- a/src/test/java/org/apache/commons/csv/CSVRecordTest.java +++ b/src/test/java/org/apache/commons/csv/CSVRecordTest.java @@ -160,6 +160,13 @@ public class CSVRecordTest { this.validateMap(map, true); } + @Test + public void testToMapWithShortRecord() throws Exception { + final CSVParser parser = CSVParser.parse("a,b", CSVFormat.DEFAULT.withHeader("A", "B", "C")); + final CSVRecord shortRec = parser.iterator().next(); + shortRec.toMap(); + } + private void validateMap(final Map map, final boolean allowsNulls) { assertTrue(map.containsKey("first")); assertTrue(map.containsKey("second"));