I have csv file like this :-
a,b,c,d //<- header
0.1,0.123,2.13,3.22
0.3,0.213,2.11,3.12
0.5,0.231,2.3,3.21
0.7,0.121,2.26,3.321
I am using apache commons csv to read the csv file.
I have made the POJO which corresponds to the above mentioned csv file and it named as CSVInputFileModel class.
Below is the method which i am using to read file: -
private List<CSVInputFileModel> inputCSVData;
void fileParser() throws IOException {
Reader in = new FileReader(INPUT_CSV_FILE);
Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(in);
for (CSVRecord record : records) {
// Adding each record to array list
CSVInputFileModel csvInputFileModel = new CSVInputFileModel();
csvInputFileModel.setA(Double.parseDouble(record.get("a")));
csvInputFileModel.setB(Double.parseDouble(record.get("b")));
csvInputFileModel.setC(Double.parseDouble(record.get("c")));
csvInputFileModel.setD(Double.parseDouble(record.get("d")));
inputCSVData.add(csvInputFileModel); //<-- NPE at second iteration
}
System.out.println(inputCSVData);
}
Below is the CSVInputFileModel class
public class CSVInputFileModel {
private double a;
private double b;
private double c;
private double d;
public CSVInputFileModel() {
}
//All args constructor
//hashcode equals
//getter setters
//toString
}
The following code gives null pointer exception. When i ran the debugger i found that in the first iteration apache csv commons is able to read the first record of csv file, while it is the second iteration which is giving null pointer exception.
I am not able to figure out what wrong i have done !.