private ArrayList<Record> readFromFile(String fileName) {
int lineNumber = 0;
Scanner fileReader;
ArrayList<Record> recs = new ArrayList<>();
try {
fileReader = new Scanner(new File(fileName));
while(fileReader.hasNextLine()) {
lineNumber++;
String line = fileReader.nextLine();
String stateCode = line.substring(0,2).trim();
String distCode = line.substring(3,8).trim();
String distName = line.substring(9,81).trim();
int population = Integer.parseInt(line.substring(82,90).trim());
int childPopulation = Integer.parseInt(line.substring(91,99).trim());
int childPovertyPopulation = Integer.parseInt(line.substring(100,108).trim());
String miscStats = line.substring(109).trim();
String[] data = new String[6];
data[0] = stateCode;
data[1] = distCode;
data[2] = distName;
data[3] = String.valueOf(population);
data[4] = String.valueOf(childPopulation);
data[5] = String.valueOf(childPovertyPopulation);
data[6] = miscStats;
recs.add(new Record(stateCode, distCode, distName, population, childPopulation, childPovertyPopulation, miscStats));
}
fileReader.close();
} catch(FileNotFoundException fnfe) {
System.out.println("Cannot locate file: " + fileName);
System.exit(0);
}
return recs;
}
the error occurs at the end of the method when I try to return "recs" with a NullPointerException thrown. Why is this happening?