An object's data is null after reading from a CSV

1

I have the following Java class:

public class Pessoa {
    private String employee_name;

    private String user_id;
    private String domain;
    private String email;
    private String role;


    @Override
    public String toString() {
        return "Pessoa [employee_name=" + employee_name + ", user_id=" + user_id + ", domain=" + domain + ", email="
                + email + ", role=" + role + "]";
    }

    // Gets e Sets omitidos para ficar menor

}

The following CSV file:

employee_name,user_id,domain,email,role
BurtonMStephenson,BMS0001,dtaa.com,[email protected],Security
Keelie M Goodwin,KMG0002,dtaa.com,[email protected],Engineer
Dara O Craig,DOC0003,dtaa.com,[email protected],VP

I have the class responsible for reading the CSV file and playing on objects of type Pessoa :

import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;



public class LerLinhasOpenCsv {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
         Reader reader = Files.newBufferedReader(Paths.get("LDAP_head.csv"));

                CsvToBean<Pessoa> csvToBean = new CsvToBeanBuilder(reader)
                    .withType(Pessoa.class).build();

            List<Pessoa> pessoas = csvToBean.parse();

            for (Pessoa pessoa : pessoas)
                System.out.println(pessoa.toString());


    }   
}

But when I print everything null appears:

Pessoa [employee_name=null, user_id=null, domain=null, email=null, role=null]
Pessoa [employee_name=null, user_id=null, domain=null, email=null, role=null]
Pessoa [employee_name=null, user_id=null, domain=null, email=null, role=null]
Pessoa [employee_name=null, user_id=null, domain=null, email=null, role=null]
Pessoa [employee_name=null, user_id=null, domain=null, email=null, role=null]
    
asked by anonymous 25.11.2018 / 06:29

1 answer

2

You should add the setters in class Pessoa :

public class Pessoa {
    ....

    public void setEmployee_name(String employee_name) {
        this.employee_name = employee_name;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    ... etc
}

Without this opencsv can not set the values, so everyone gets null . By adding the setters , the values are filled in correctly:

Pessoa [employee_name=BurtonMStephenson, user_id=BMS0001, domain=dtaa.com, [email protected], role=Security]
Pessoa [employee_name=Keelie M Goodwin, user_id=KMG0002, domain=dtaa.com, [email protected], role=Engineer]
Pessoa [employee_name=Dara O Craig, user_id=DOC0003, domain=dtaa.com, [email protected], role=VP]

Another detail is to use Java code conventions and avoid _ in field names, and instead use camelCase : employeeName instead of employee_name and userId instead of user_id .

I understand that you have done this so that the field names in the CSV file match the field names of the class. But you can use different names and map them with the @CsvBindByName annotation.

The detail is that by putting this annotation in a field, I had to put it in everyone. When the field has a different name in the CSV file, I put this in the annotation. When the name is the same, just put the annotation without parameters.

public class Pessoa {

    // employeeName corresponde à coluna employee_name do arquivo
    @CsvBindByName(column = "employee_name")
    private String employeeName;

    // userId corresponde à coluna user_id do arquivo
    @CsvBindByName(column = "user_id")
    private String userId;

    // campos domain, email e role tem o mesmo nome no CSV (usar anotação sem parâmetros)

    @CsvBindByName
    private String domain;

    @CsvBindByName
    private String email;

    @CsvBindByName
    private String role;

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    .... // demais setters para os outros campos
}
    
26.11.2018 / 12:48