I have a method readFromFile like this:
private static ArrayList<ArrayList<Integer>> readFromFile(String name) {
ArrayList<ArrayList<Integer>> inputs = new ArrayList<ArrayList<Integer>>();
try {
InputStream in = ReadWriteFile.class.getClass().getResourceAsStream(name);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
ArrayList<Integer> input = new ArrayList<Integer>();
for (int i=0; i<line.length(); i++) {
int value = 0;
try {
value = Integer.parseInt(String.valueOf(line.charAt(i)));
} catch (Exception e) {
input.add(value);
}
inputs.add(input);
}
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return inputs;
}
and I use this method here to read a folder of txt files. I need it to put the names of files in an arraylist (the files are an alphabet):
public static ArrayList<TrainingSet> readTrainingSets() {
ArrayList<TrainingSet> trainingSets = new ArrayList<TrainingSet>();
for (int i=0; i<26; i++) {
char letterValue = (char) (i+26);
String letter = String.valueOf(letterValue);
for (ArrayList<Integer> list : readFromFile("C:\\Users\\hp\\Desktop\\resources\\" + letter + ".txt")) {
trainingSets.add(new TrainingSet(list, GoodOutputs.getInstance().getGoodOutput(letter)));
}
}
return trainingSets;
}
The source folder includes files like: A.txt, B.txt, etc. When I try to run the code I get an error.
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at data.ReadWriteFile.readFromFile(ReadWriteFile.java:34)
at data.ReadWriteFile.readTrainingSets(ReadWriteFile.java:22)
at network.Train.<init>(Train.java:16)
at gui.MainGui.<init>(MainGui.java:38)
at gui.MainGui.main(MainGui.java:32)
I guess the method can't find the files on my computer. But I don't know why. Is the form wrong? Can you help me?