I want to write a csv with the opencsv
libary, however, when running the code I get a NullPointerException
.
public void exportToCSV(ArrayList<Data> list) throws IOException {
log.info("write CSV file");
String writerPath = "C:\\Users\\User\\Desktop\\Output\\output.csv";
CSVWriter writer = new CSVWriter(new FileWriter(writerPath), ';');
//headers
String [] entries = {"ID", "Date"};
writer.writeNext(entries);
List<String[]> data = new ArrayList<String[]>();
for (int m = 0; m < list.size(); m++) {
data.add(new String[] {
list.get(m).getID,
(list.get(m).getDate().toString()==null) ? "null" : list.get(m).getDate().toString(), //Here i get the NullPointerException
});
}
writer.writeAll(data);
writer.close();
}
I guess that getDate()
is null, which type is a Timestamp
. However, why does my proposed solution not work in writing a String
when getDate()
is null
.
I apprecaite your reply!