Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

jake jake
Courses Plus Student 812 Pointsmy app keeps crashing
I'm trying to make a math app where you have to type numbers on separate lines to get a total. I already put the equation in the coding but if I don't but a number in one of the lines and I hit the sum button the app says there was an error and shuts itself down.
how do I fix this? I tried making an if statement but it didn't work
if I put a 0 instead of nothing it works but I don't want the users to have to put a number in if they don't need to
Daniel Hair
5,080 PointsTo do this, you would go here http://developer.android.com/reference/java/util/regex/Pattern.html, which tells you about the class.
However, I guess I should ask, what are you using to ignore newlines or spaces, or how are you processing each line to check the sum? Can you give me your code snippet?

jake jake
Courses Plus Student 812 Points protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tal_calculator);
}
public void OnButtonClick (View v) {
int n1;
int n2;
int n3;
int n4;
int n5;
int n6;
int n7;
int n8;
int n9;
int n10;
int thesum;
int x;
String total;
EditText e1 = (EditText) findViewById(R.id.num1);
EditText e2 = (EditText) findViewById(R.id.num2);
EditText e3 = (EditText) findViewById(R.id.num3);
EditText e4 = (EditText) findViewById(R.id.num4);
EditText e5 = (EditText) findViewById(R.id.num5);
EditText e6 = (EditText) findViewById(R.id.num6);
EditText e7 = (EditText) findViewById(R.id.num7);
EditText e8 = (EditText) findViewById(R.id.num8);
EditText e9 = (EditText) findViewById(R.id.num9);
EditText e10 = (EditText) findViewById(R.id.num10);
TextView t1 = (TextView)findViewById(R.id.sum);
n1 = Integer.parseInt(e1.getText().toString());
n2 = Integer.parseInt(e2.getText().toString());
n3 = Integer.parseInt(e3.getText().toString());
n4 = Integer.parseInt(e4.getText().toString());
n5 = Integer.parseInt(e5.getText().toString());
n6 = Integer.parseInt(e6.getText().toString());
n7 = Integer.parseInt(e7.getText().toString());
n8 = Integer.parseInt(e8.getText().toString());
n9 = Integer.parseInt(e9.getText().toString());
n10 = Integer.parseInt(e10.getText().toString());
x = ((n1 - n2 - n3 - n4 - n5 - n6 - n7) / (n8 + n9 + n10));
thesum = (x/10 * 100);
total = thesum + "%";
t1.setText(Integer.toString(Integer.parseInt(total)));
}```
5 Answers
Daniel Hair
5,080 PointsFor that issue, yes, Kristen, that is right on the dot. I decided to make this app to check out the real concerns. When I tried it like yours Jake, I had a lot of debugging to do. Because of a few things. I imagine that since you had this: "public void OnButtonClick (View v)" method, that you had this function connected to your Button. I did that, but I think that was one of your main problems. The reason is, is because although you click on it, your onCreate method is not listening for the button click. For example, this is my setup and it works. Mind you, I have a few different names and different ways, but just understand the point, which is the onCreate Method. The onCreate Method needs to be the function to initialize all the Buttons, EditText Views, etc (for this app), along with having a listener. Just read the comments:
public class MathApp extends Activity {
public static final String TAG = MathApp.class.getSimpleName();
protected EditText e1;
...
protected EditText e10;
protected TextView t1;
protected Button buttonSum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_math_app);
buttonSum = (Button) findViewById(R.id.buttonSum);
t1 = (TextView) findViewById(R.id.textSum);
e1 = (EditText) findViewById(R.id.editText);
...
e10 = (EditText) findViewById(R.id.editText10);
//This "setOnClickListener" method is most important. This will allow the button to work when clicked.
buttonSum.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
int[] n = new int[10];
for (int i = 0; i < n.length; ++i) {
n[i] = 0; //initialize each int to zero (of course, JAVA does this automatically, but just good practice)
}
if (!e1.getText().toString().equals("")) { // OR "!e1.getText().toString().isEmpty()"
n[0] = Integer.parseInt(e1.getText().toString());
}
...
if (!e10.getText().toString().equals("")) {
n[9] = Integer.parseInt(e10.getText().toString());
}
//I only had the sum int instead of three different ints, just a preference
//BTW, as you might notice, I did not calculate it like yours, I was just messing around with the numbers
int sum = ((n[0] + n[1] + n[2] + n[3] + n[4] + n[5] + n[6]) + (n[7] + n[8] + n[9]));
sum = ((sum / 10) / 100);
String total = sum + "%";
t1.setText(total); //There is no need to change this to an int, seeing as the setText method asks for a string.
t1.setVisibility(View.VISIBLE); //Show the text; In my xml, I make this textView invisible by default
}
}
);
}
}
Hope this helps! Sorry about it being so long

jake jake
Courses Plus Student 812 Pointshey thanks a lot for all the help I have been trying to mess around with it for a while and this got me a lot closer but I still cant manage to get the app to work. if I leave even one line blank the app still crashes and I have to start it back up. is it possible I just need to put a return class in the coding?

Kristen Law
16,244 PointsHi Jake! You may be getting an error because you are calling the parseInt
method on an empty string. If you don't put a number in one of the lines, (let's say EditText e1
is empty, for example) then when your program calls Integer.parseInt(e1.getText().toString())
, it is trying to parse an empty string.
To resolve this, you can add if
statements around each of n1
through n10
to check if e1
through e10
are empty or not. If the EditText
is not empty, then you can go ahead with the call to parseInt
.
You will also need to initialize int n1
through n10
to 0, since you use them in the following line:
x = ((n1 - n2 - n3 - n4 - n5 - n6 - n7) / (n8 + n9 + n10));
If EditText e1
is empty, for example, then n1
will never be initialized. This will cause an error when you try to use n1
in the above line. Therefore, you should set it equal to 0 when you declare it.
Hope that helps! Feel free to ask any questions if you need to clarify.
Daniel Hair
5,080 PointsKristen, I think you are more correct than I. Now seeing your code Jake, I think I have a much better idea on how to solve it.
Have you tried adding checks for null or non-integer fields? For example:
if (n1 == null || n1 == (int) n1) { n1 = 0; } else { n1 = Integer.parseInt(e1.getText().toString()); }
You can do this for each case. This way it assigns it to 0 if there was no input (null), or if the variable is not an integer. Hope this helps. (P.S. I did not make sure this worked, this is just the idea I am trying to give you)

jake jake
Courses Plus Student 812 Pointshey thanks for the help but it still isn't working I tried making an if statement myself and it didn't work so I tried Daniel's line and it wouldn't except the beginning part " (n1 == null " I also tried declaring each int as equal to 0 but it didn't do anything the app still crashed
Daniel Hair
5,080 PointsMy apologies, the null was for e1. But let me create it on my computer and will give you an accurate answer

Kristen Law
16,244 PointsYou will need to set each of n1
through n10
equal to 0, as well as add the if
statements. If you do not add the if
statements, then when it gets to the lines where you assign whatever text is in the EditText
s, it will still result in an error if it is empty.
In your if
statement, you can either check to see if the EditText
is not the empty string "", or if its size is greater than 0. This is what one of your if
statements could look like:
if (!e1.getText().toString().equals("")) {
n1 = Integer.parseInt(e1.getText().toString());
}
OR
if (e1.getText().toString().trim().length() > 0) {
n1 = Integer.parseInt(e1.getText().toString().trim());
}
In the second option, I called the trim()
method on your string. It basically removes any blank space before or after your intended string. You can check out the Java documentation on trim()
here: https://docs.oracle.com/javase/6/docs/api/java/lang/String.html#trim().
In my opinion, the second one is better, but you can choose whichever one you think best suits your needs. Also, keep in mind that you will receive an error if the user enters something that is not a number, but you can always add a check for that later.
You should add an if
statement around each of the lines assigning integers to n1
through n10
, just like above.
Hope that helps!
Daniel Hair
5,080 PointsSorry about replying slowly. Could you show me an example of what you have again, and then what your proposed fix is?

jake jake
Courses Plus Student 812 Pointshey Daniel thanks for all the help but I finally figured it out the problem was that I was trying to cast the string to an integer when the line is blank and does not have a value to convert which was causing the error
Daniel Hair
5,080 PointsDaniel Hair
5,080 PointsI'm wondering if this is because if goes through each line and if there isn't anything, then it crashes, because there isn't a check for new line. Perhaps, use a Pattern class that uses regex to check for newlines and spaces, etc?