Reputation: 1139
Given the following example csv data
name,address,sibling,sibling,sibling
John,MainSt,Sarah,Mike,Greg
and the example POJO that I want to parse the data into
public class Employee {
private String name;
private String address;
private List<String> siblings;
public Employee() { }
public void setName(String name) { ... }
public void setAddress(String address { ... }
public void setSiblings(List<String> siblings) { ... }
}
and the following field mapping
String[] fieldMapping = new String[]{
"name",
"address",
"siblings[0]",
"siblings[1]",
"siblings[2]"
}
and the following cell processors
CellProcessors[] processors = new CellProcessors[]{
new NotNull(), // name
new NotNull(), // address
new NotNull(), // siblings[0]
new NotNull(), // siblings[1]
new NotNull() // siblings[2]
}
I would expect to be able to parse the csv data into Employee
without a problem, however I am receiving the following exception
org.supercsv.exception.SuperCsvReflectionException: unable to find method setSiblings[0](java.lang.String) in class com.Employee - check that the corresponding nameMapping element matches the field name in the bean, and the cell processor returns a type compatible with the field
This is how I do the actual parsing
List<Employee> deserializedRecords = new ArrayList<>();
try (ICsvBeanReader beanReader = new CsvBeanReader(new FileReader(file), CsvPreferences.STANDARD_PREFERENCE)) {
beanReader.getHeader(true);
Employee model;
while ((model = (Employee) beanReader.read(Employee.class, fieldMapping, processors)) != null) {
deserializedRecords.add(model);
}
}
This pretty much follows the answer given here, and after reading the SuperCSV documentation, I'm not entirely sure why the exception is getting thrown.
Upvotes: 1
Views: 778
Reputation: 9868
You're using CsvBeanReader
which doesn't support indexed (or deep) mapping. You're looking for CsvDozerBeanReader
(examples here).
Upvotes: 2