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]
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– hkotsubo
Thank you very much friend, that was right, I ended up not implementing the setters. Thank you.
– Joelson Pereira
@hkotsubo formalize your reply and Joelson marks it with resolved when the author of the reply writes it.
– Adriano Gomes