1

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 !.

AConsumer
  • 2,461
  • 2
  • 25
  • 33

2 Answers2

2

First, i'm sure that you are reading the first record successfully but is not possible that could be added in the list, so:

  • You are using Enhaced for loop, which means the second record is there and that's not your problem.
  • The CSVInputFileModel seems to be right.
  • You declared the inputCSVData but is not instantiated.

my conclusion is you have missed the input list instance, that's why enters the first iteration, but never the second.

to solve this put inputCSVData = new ArrayList<CSVInputFileModel>() before trying to add elements in your code to fix it.

Rcordoval
  • 1,932
  • 2
  • 19
  • 25
2

I have used your code with required changes and it's working fine.

public class Test {
public static void main(String[] args) throws IOException {
    fileParser();
}

static void fileParser() throws IOException {
    Reader in = new FileReader("abc.csv");
    Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(in);
    List<CSVInputFileModel> inputCSVData = new ArrayList<>();
    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
    }
    for (CSVInputFileModel data : inputCSVData) {
        System.out.println(data.getA() + ":" + data.getB() + ":" + data.getC() + ":" + data.getD());
    }
}

}

Sanjay
  • 2,481
  • 1
  • 13
  • 28