I am very new to android development with a small amount of Java programming knowledge. I need help! I am making an app that allows you to enter the amount of money you spend and it calculates how much more you can spend. It also keeps track of the budget that you have left inside of a text file. It is made in android studio using Java.
Here is my error message:
2019-03-03 09:42:14.245 4178-4178/? E/SPPClientService: [PackageInfoChangeReceiver] [handlePkgRemovedEvent] PackageName : com.concretegames.budgettracker, true, false
2019-03-03 09:42:27.573 16961-16961/com.concretegames.budgettracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.concretegames.budgettracker, PID: 16961
java.lang.NumberFormatException: s == null
at java.lang.Integer.parseInt(Integer.java:570)
at java.lang.Integer.parseInt(Integer.java:643)
at com.concretegames.budgettracker.MainActivity$1.onClick(MainActivity.java:56)
at android.view.View.performClick(View.java:6935)
at android.widget.TextView.performClick(TextView.java:12738)
at android.view.View$PerformClick.run(View.java:26211)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
And here is my code:
package com.concretegames.budgettracker;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView response = findViewById(R.id.response);
final Button calc = findViewById(R.id.Calculate);
Context context = this;
String filepath = "total_expense.txt";
String filestoragepath = "MyFileStorage";
final File edit_file = new File(getExternalFilesDir(filestoragepath), filepath);
EditText $box = findViewById(R.id.expenses);
final String $ = $box.getText().toString();
calc.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (edit_file.exists() && !edit_file.isDirectory()) {
try {
FileReader fr = new FileReader(edit_file);
BufferedReader br = new BufferedReader(fr);
String str;
while ((str = br.readLine()) != null) {
out.println(str + "\n");
}
br.close();
int result = Integer.parseInt(str) - Integer.parseInt($);
FileWriter fw = new FileWriter(edit_file);
PrintWriter pw = new PrintWriter(fw);
pw.print("");
pw.print(result);
pw.close();
response.setText("Budget left over: "+result);
} catch (IOException e) {
out.println("ERROR: " + e.toString());
response.setText("ERROR: " + e.toString());
}
} else {
try {
edit_file.createNewFile();
FileWriter fw = new FileWriter(edit_file);
PrintWriter pw = new PrintWriter(fw);
pw.print("1500");
pw.close();
} catch (IOException e) {
out.println("ERROR: " + e.toString());
response.setText("ERROR: " + e.toString());
}
}
}
});
}
}
P.S. I'm only 13 and don't have experience with Java. I prefer HTML, python, and javascript. Any help is appreciated!
Integer.parseInt() throws an exception if it can't successfully parse the String into an int. But the exception that is thrown is a subclass of RuntimeException, so the java compiler does not force you to catch the exception. But it's still highly suggested that you do catch it. And the crash will be avoided. In general, do something like this when parsing Strings into ints:
int result;
try {
result = Integer.parseInt(someString);
} catch (NumberFormatException e) {
result = 0;
}