Answer the question
In order to leave comments, you need to log in
Why can't the application open the file for reading?
There is an application that, when you click on the "Save" button, writes two values \u200b\u200binto a file.
When you click on the "Read" button, it should read the file and select these values from the line.
When saving, there are no errors, and a message is displayed in the console that the save was successful.
But when reading, the application crashes on line 1.
private String M_PREFS = "Lab7";
private String FILE_NAME = "font_settings2";
private String loadViewSettings() {
String str = "";
try {
FileInputStream file = openFileInput(FILE_NAME);
BufferedReader br = new BufferedReader(new InputStreamReader(file)); // 1 Тут происходит краш
while ((str = br.readLine()) != null) {
Log.d(M_PREFS, str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
private void saveViewSettings() {
try {
getApplicationContext().deleteFile(FILE_NAME);
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(openFileOutput(FILE_NAME, MODE_PRIVATE))
);
int color, sise;
switch (this.color) {
case Color.RED:
color = 0;
break;
case Color.BLUE:
color = 1;
break;
default:
color = 2;
}
switch (this.size) {
case 33:
size = 0;
break;
case 23:
size = 1;
break;
default:
size = 2;
}
bw.write(String.format("%d%d", color, size));
bw.close();
Log.d(M_PREFS, "Файл записан");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: ru.knastu.tempfct.livenlab7, PID: 9579
java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at ru.knastu.tempfct.livenlab7.MainActivity.onClick(MainActivity.java:214)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11185)
at android.view.View$PerformClick.run(View.java:23752)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question