I'm attempting to access data from CSV file (which I downloaded from EBay's MIP server) using Apache Commons CSV however I'm encountering the following error:
java.lang.IllegalArgumentException: Index for header 'Selected Category ID' is 4 but CSVRecord only has 1 values!
I'm not quite sure the cause since the file clearly contains this index. My CSV file looks like this:
I'm using the following code to access file:
CSVParser csvParser = null;
String selectedCategoryIDFieldName = "Selected Category ID";
try {
Reader reader = Files.newBufferedReader(Paths.get(CSVFile));
csvParser = new CSVParser(reader, CSVFormat.DEFAULT
.withHeader("SKU", "Locale", "Title", "Channel", selectedCategoryIDFieldName)
.withIgnoreHeaderCase()
.withTrim()
.withSkipHeaderRecord(true));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (csvParser != null) {
List<CSVRecord> csvRecords = csvParser.getRecords();
for (CSVRecord csvRecord : csvRecords) {
// Accessing values by the names assigned to each column
try {
long currentRecordNumber = csvRecord.getRecordNumber();
String SKU = csvRecord.get("SKU");
String categoryID = csvRecord.get(selectedCategoryIDFieldName);
// ^^ this line throws `IllegalArgumentException`
System.out.println("Current record number: " + currentRecordNumber);
System.out.println("SKU - > " + SKU);
System.out.println("categoyrID -> " + categoryID);
} catch (Exception e) {
e.printStackTrace();
}
}
I searched around SO and the closest question I found is here but it is not related to my question as the format is exactly the same before/after I save it (In other words, I don't see anything wrong with the format like in the other users question).
Update: I just realized this error surfaces on the second iteration of the for (CSVRecord csvRecord : csvRecords)
loop (the file only contains one record). However, I still don't understand why it is iterating twice if there's only one record in CSV file.. and why does it only surface by category ID and not by SKU column?