I've got a multiple student objects I want to write into with a CSV file containing their details. I've set each row of the CSV file to an array then was going to split each entry of the array into another array and use that to set the attributes of the object. However, each time I try, I get a NullPointerException.
String studentCSV = "src\\CSV Files\\Students.csv";
Student[] student = new Student[CSV_Reader.count(studentCSV)];
String[] values = CSV_Reader.read(studentCSV);
for(int i=0;i<values.length;i ){
String[] line = values[i].split(",");
student[i].addPerson(line[0],line[1],line[2],line[3]);
student[i].addStudent(line[4],line[5],line[6]);
}
CodePudding user response:
int n=10; // for example
Student[] student = new Student[n];
//now you just allocate memory for array
for(int i=0;i<student.length;i ){
student[i]=new Student();
// here you assign student to your any element of array
}
// now you can do anything with elements of your student array
CodePudding user response:
Most likely, there is a line with information (or separator) missing and trying to access that index causes the exception. You should check that first (simply print out the lines in the loop at first and whatever causes the error will be the last to print).
Otherwise: please show the full error log and point out on which line the error occurs.