Skip to content

Instantly share code, notes, and snippets.

@egmanoj
Last active December 10, 2015 04:18
Show Gist options
  • Save egmanoj/4379763 to your computer and use it in GitHub Desktop.
Save egmanoj/4379763 to your computer and use it in GitHub Desktop.
package supercsv.test;
import static org.supercsv.prefs.CsvPreference.STANDARD_PREFERENCE;
import java.io.IOException;
import java.io.PrintWriter;
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.dozer.CsvDozerBeanWriter;
import org.supercsv.io.dozer.ICsvDozerBeanWriter;
public class DozerBugTest {
private static final String[] FIELD_MAPPING = new String[]{ "firstName",
"middleName", "lastName" };
private static final CellProcessor[] PROCESSORS = new CellProcessor[]{
new Optional(), new Optional(), new Optional() };
private static final String[] HEADER = new String[]{ "First_Name",
"Middle_Name", "Last_Name" };
public static void main(String[] args) throws Exception {
Person person = new Person();
person.setFirstName("The Joker");
person.setMiddleName(null);
person.setLastName(""); // Empty String
ICsvDozerBeanWriter beanWriter = null;
try {
beanWriter = new CsvDozerBeanWriter(new PrintWriter(System.out),
STANDARD_PREFERENCE);
beanWriter.configureBeanMapping(Person.class, FIELD_MAPPING);
beanWriter.writeHeader(HEADER);
beanWriter.write(person, PROCESSORS);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (beanWriter != null) {
beanWriter.close();
}
}
}
}
package supercsv.test;
public class Person {
private String firstName;
private String middleName;
private String lastName;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getMiddleName() {
return middleName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
}
First_Name,Middle_Name,Last_Name
Exception in thread "main" org.supercsv.exception.SuperCsvException: The number of columns to be processed (2) must match the number of CellProcessors (3): check that the number of CellProcessors you have defined matches the expected number of columns being read/written
context={lineNo=2, rowNo=2, columnNo=1, rowSource=[The Joker, null]}
at org.supercsv.util.Util.executeCellProcessors(Util.java:78)
at org.supercsv.io.dozer.CsvDozerBeanWriter.write(CsvDozerBeanWriter.java:133)
at supercsv.test.DozerBugTest.main(DozerBugTest.java:36)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment