1

this weekend I tried out your great framework and would like to use it in production. Unfortunately my Employee Bean has an attribute sexuality which is of type enum. Somehow the integrated official ParseEnum cellprocessor cannot parse the sexuality attribute while reading from .csv file. Could you please look into this?

Error Message

Exception in thread "main" org.supercsv.exception.SuperCsvCellProcessorException: ' BI' could not be parsed as a enum of type test.supercsv.Sexuality
processor=org.supercsv.cellprocessor.ParseEnum
context={lineNo=2, rowNo=2, columnNo=5, rowSource=[1, Pankaj Kumar, CEO, 5,000USD,  BI]}
    at org.supercsv.cellprocessor.ParseEnum.execute(ParseEnum.java:146)
    at org.supercsv.util.Util.executeCellProcessors(Util.java:93)
    at org.supercsv.io.AbstractCsvReader.executeProcessors(AbstractCsvReader.java:203)
    at org.supercsv.io.CsvBeanReader.readIntoBean(CsvBeanReader.java:261)
    at org.supercsv.io.CsvBeanReader.read(CsvBeanReader.java:190)
    at test.supercsv.Reading.readWithCsvBeanReader(Reading.java:35)
    at test.supercsv.Reading.main(Reading.java:24)

PARTIAL CODE

enum CellProcessor

 private static CellProcessor[] getProcessors() {

        final CellProcessor[] processors = new CellProcessor[]{
            new UniqueHashCode(), // ID (must be unique)
            new NotNull(), // Name
            new Optional(), // Role
            new NotNull(), // Salary
            new ParseEnum(Sexuality.class, true) // enum Sexuality
        };

        return processors;
    }

First Employee object from csv file

1,Pankaj Kumar,CEO,"5,000USD", BI

FULL CODE

Employee Bean

package test.supercsv;

public class Employee {

    private String id;
    private String name;
    private String role;
    private String salary;
    private Sexuality sexuality;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getSalary() {
        return salary;
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }

    public Sexuality getSexuality() {
        return sexuality;
    }

    public void setSexuality(Sexuality sexuality) {
        this.sexuality = sexuality;
    }

    @Override
    public String toString() {
        return "Employee{" + "id=" + id + ", name=" + name + ", role=" + role + ", salary=" + salary
                + ", sexuality=" + sexuality.name()
                + '}';
    }

}

enum Sexuality

package test.supercsv;

public enum Sexuality {

    HETERO, HOMO, BI, TRANSGENDER;

}

CSVBeanReader

package test.supercsv;

import java.io.FileReader;
import java.io.IOException;
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseEnum;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.constraint.UniqueHashCode;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.prefs.CsvPreference;

public class Reading {

    private static final String CSV_FILENAME = "src/resources/employee.csv";

    public static void main(String[] args) throws IOException {
        readWithCsvBeanReader();
    }

    private static void readWithCsvBeanReader() throws IOException {
        try (ICsvBeanReader beanReader = new CsvBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE)) {

            // the header elements are used to map the values to the bean (names must match)
            final String[] header = beanReader.getHeader(true);
            final CellProcessor[] processors = getProcessors();

            Employee employee;
            while ((employee = beanReader.read(Employee.class, header, processors)) != null) {
                System.out.println(String.format("lineNo=%s, rowNo=%s, employee=%s",
                        beanReader.getLineNumber(),
                        beanReader.getRowNumber(),
                        employee));
            }

        }
    }

    private static CellProcessor[] getProcessors() {

        final CellProcessor[] processors = new CellProcessor[]{
            new UniqueHashCode(), // ID (must be unique)
            new NotNull(), // Name
            new Optional(), // Role
            new NotNull(), // Salary
            new ParseEnum(Sexuality.class, true) // enum Sexuality
        };

        return processors;
    }
}

content of CSV-file

ID,Name,Role,Salary,Sexuality
1,Pankaj Kumar,CEO,"5,000USD", BI
2,Lisa,Manager,500USD, Homo
3,David,,1000USD, Hetero
Chiggiddi
  • 542
  • 1
  • 8
  • 26
  • 1
    Have you tried removing the spaces in the last column (sexuality)? Spaces are regarded as part of the data in CSV. If you don't have control over the CSV file, you can use the `new Trim(new ParseEnum())` – James Bassett Aug 15 '16 at 10:03
  • Dog, ur awesome! YES, that was the cause! `spaces` caused the trouble! I added a solution in which you are mentioned for future references. – Chiggiddi Aug 15 '16 at 11:57

1 Answers1

1

Solution

just as Hound Dog has pointed out: the spaces in my csv-file were the cause of trouble. Just erase them and code works now!

ID,Name,Role,Salary,Sexuality
1,Pankaj Kumar,CEO,"5,000USD,BI
2,Lisa,Manager,500USD,Homo
3,David,,1000USD,Hetero
Chiggiddi
  • 542
  • 1
  • 8
  • 26
  • What if we have List of Enum ? I was not able to parse that.But it is working properly with type string but not with type enum with this @CsvBindAndSplitByPosition – ammy May 31 '20 at 17:07
  • What is the purpose to have a list of enums in your class? Usually, a class has multiple enums but not a list of enums. – Chiggiddi Jun 02 '20 at 06:18