Title: CommonCsv : IllegalArgException when during record.get when particular file is used
Tags: java;apache-commons
Question: I have the following:

StateCountryDataLoader.java

```private Map<String, String&gt; getStateCountryMap() throws IOException {
    final Map<String, String&gt; stateCountryMap = new HashMap<&gt;();

    try (final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(getFile
            (filePath)), StandardCharsets.UTF_8));
            final CSVParser parser = new CSVParser(reader, CSVFormat.TDF.withHeader("STT_CDE", "CTRY_CDE")
                    .withSkipHeaderRecord())) {

        for(CSVRecord r : parser) {
            stateCountryMap.put(r.get("STT_CDE"), getValueOrDefault(r.get("CTRY_CDE")));//get or default is an isNotBlank check. if true, return empty string, otherwise return the value provided to it
        }
    }

    return stateCountryMap;
}

private File getFile(String path) throws IOException {
        return new ClassPathResource(path).getFile();
}
```

And this works fine when the file used is the following:

STT.txt

```STT_CDE CTRY_CDE
AA  USA
AB  CAN
AE  USA
AK  USA
AL  USA
AP  USA
AR  USA
AZ  USA
BC  CAN
CA  USA
CO  USA
CT  USA
DC  USA
DE  USA
FL  USA
FR  
GA  USA
GU  USA
HI  USA
IA  USA
ID  USA
IL  USA
IN  USA
KS  USA
KY  USA
LA  USA
MA  USA
MB  CAN
MD  USA
ME  USA
MI  USA
MN  USA
MO  USA
MS  USA
MT  USA
NB  CAN
NC  USA
ND  USA
NE  USA
NH  USA
NJ  USA
NL  CAN
NM  USA
NS  CAN
NT  CAN
NU  CAN
NV  USA
NY  USA
OH  USA
OK  USA
ON  CAN
OR  USA
PA  USA
PE  CAN
PR  USA
QC  CAN
RI  USA
SC  USA
SD  USA
SK  CAN
TN  USA
TX  USA
UT  USA
VA  USA
VI  USA
VT  USA
WA  USA
WI  USA
WV  USA
WY  USA
YT  CAN
```

I have no issues when this is the case; the record.get methods function as expeted, even for the line that includes a blank CTRY_CDE value. But when i use the following file:

sample.txt

```STT_CDE CTRY_CDE
AA  USA
AB  CAN
FR  
```

I get the following:

```Caused by: java.lang.IllegalArgumentException: Index for header 'CTRY_CDE' is 1 but CSVRecord only has 1 values!
    at org.apache.commons.csv.CSVRecord.get(CSVRecord.java:108)
    at com.lmig.ci.fnol.bo.assign.dataload.StateValidValueLoad.getStateCountryMap(StateValidValueLoad.java:83)
etc...
```

Worth nothing, that should I remove the FR line, then everything runs as expected, and when i open the file in Intellij after the test has ran, the tab after FR has disappeared, so im not sure if its an issue with how im reading in the file or not, but I don't see what the issue is, given the same line is present in the full file, and any unit tests or calls to this method don't throw the same exception.

I'm quite new to CommonCsv so I'm not sure if whats occurring is due to inexperience(maybe there is a setting that I've failed to check that would avoid this issue), or if something else is at play here regarding how the file is read in, but I can't get to the bottom of this

Comment: Do the file that fails to parse ends with a linefeed? If it doesn't, I'd try adding one, maybe the parser would handle running into a `\n` better than in an EOF when it expects a value.

Comment: Glad to see that fixed it !

Comment: @Aaron unsure, will check this out. If it adds anything, I've noticed in the full file the tab is preserved but every time i move rows in the smaller sample, the tab is removed

Comment: @Aaron, so yeah, Intellij making life difficult. I didn't realise that on save it would strip out trailing whitespace and tabs, changing this setting fixed the issue



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:09/18/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><apache-commons>', 'Title': 'CommonCsv : IllegalArgException when during record.get when particular file is used', 'ViewCount': 240, 'id': 52388296, 'user_AboutMe': '<p>Software Engineer of 5 years expereince</p>\n\n<p>Experience with Java SE(multiple versions up to 8), Spring Boot + Spring MVC and various elements of Spring ecosystem, XSLT, Javascript, Thymeleaf, Sql/No-Sql(MongoDb), JUnit 4 + 5, Apache Camel, MyBatis and various other minor libraries (testing and src code development)</p>\n\n<h1>SOreadytohelp</h1>\n', 'user_AccountId': 4852300.0, 'user_DisplayName': 'jbailie1991', 'user_Reputation': 1285.0, 'user_Views': 179.0, 'user_id': 3914282.0}}