The question is very simple: I have those who methods in my class. This one saves the data in a text file:
//Store the data in a text file
public void saveScores(String testo, TextView text) {
try {
FileOutputStream fileout = getActivity().openFileOutput("flagQuiz.txt", getActivity().MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(testo);
outputWriter.close();
text.setText(testo);
} catch (Exception e) {
e.printStackTrace();
}
}
This method instead reads the data from a text file.
//Load data from text file
public void loadScores() {
try {
FileInputStream fileIn = getActivity().openFileInput("flagQuiz.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);
char[] inputBuffer= new char[100];
String s="";
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
} catch (Exception e) {
e.printStackTrace();
}
}
From what I can gauge the first method works pretty well because it doens't raise exceptions and the text.setText(testo);
works fine.
PROBLEM
I cannot read the text file when the app opens (onCreate).
As you can see here there is a null pointer exception, which means I guess that the file is not saved or I am typing a wrong path for the InputStream maybe.
Any suggestion about this? I am going to write this very little text file on the internal storage.
The complete log can be found here: link.