Data from an object is null after reading from a CSV

Asked

Viewed 122 times

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 the 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 make the impression it’s all there 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]
Pessoa [employee_name=null, user_id=null, domain=null, email=null, role=null]
  • 1

    I did a test here with opencsv 4.0 and the data is only null if the class Pessoa did not have the setters. I put the setters in it and it worked, the data is filled and none is null. Please check if this is it or if there are any other details you haven’t mentioned - and if so, just click on [Edit] and add this information to the question

  • Thank you very much friend, that was right, I ended up not implementing the setters. Thank you.

  • @hkotsubo formalize your reply and Joelson marks it with resolved when the author of the reply writes it.

1 answer

2


You must 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 cannot set the values, and so everyone is null. 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 the 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 names of the class fields. But you can use different names and map them with the annotation @CsvBindByName.

The thing is, when I put this note 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
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.