I need to load
data from a .txt file
by using the following method:
public void getTextFromFile()
{
File path = getExternalFilesDir(null);
File file = new File(path, "alarmString.txt");
int length = (int) file.length();
byte[] bytes = new byte[length];
FileInputStream in = null;
try {
in = new FileInputStream(file);
in.read(bytes);
in.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
String contents = new String(bytes);
TextView clockTxt = (TextView) findViewById(R.id.clockText);
assert clockTxt != null;
clockTxt.setText(contents);
}
}
When calling the method getTextfromFile();
, under the onCreate();
the program crashes, with giving me the following error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
I have tried to:
Check if the
id
is correct.Made sure that the correct
layout
is being called bysetContentView(R.id.activity_main);
Making sure that the method
getTextFromFile();
is being called under thesetContentView()
;
Thanks!