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

Android

I'm trying to figure out how to stop my app from crashing

I'm building a math app and every time I run my app if I leave one of the EditText lines blank the app crashes I tried to use if statements but I cant get it to work

public void OnButtonClick(View v) {

        final EditText e1 = (EditText) findViewById(R.id.num1);
        final EditText e2 = (EditText) findViewById(R.id.num2);
        final EditText e3 = (EditText) findViewById(R.id.num3);
        final EditText e4 = (EditText) findViewById(R.id.num4);
        final EditText e5 = (EditText) findViewById(R.id.num5);
        final EditText e6 = (EditText) findViewById(R.id.num6);
        final EditText e7 = (EditText) findViewById(R.id.num7);
        final EditText e8 = (EditText) findViewById(R.id.num8);
        final EditText e9 = (EditText) findViewById(R.id.num9);
        final EditText e10 = (EditText) findViewById(R.id.num10);
        final TextView t1 = (TextView) findViewById(R.id.sum);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override   public void onClick(View v) {
                int n1;
                int n2;
                int n3;
                int n4;
                int n5;
                int n6;
                int n7;
                int n8;
                int n9;
                int n10;
                double thesum;
                double x;
                String total;


                int[] n = new int[10];
                for (int i = 0; i < n.length; ++i) n[i] = 0;

                n1 = Integer.parseInt(e1.getText().toString());
                if (e1.getText().toString().isEmpty())
                  n1 = Integer.parseInt(e1.getText().toString());
                n2 = Integer.parseInt(e2.getText().toString());
                if (e2.getText().toString().equals(""))
                    n2 = Integer.parseInt(e10.getText().toString());
                n3 = Integer.parseInt(e3.getText().toString());
                if (e3.getText().toString().equals(""))
                    n3 = Integer.parseInt(e10.getText().toString());
                n4 = Integer.parseInt(e4.getText().toString());
                if (e4.getText().toString().equals(""))
                    n4 = Integer.parseInt(e10.getText().toString());
                n5 = Integer.parseInt(e5.getText().toString());
                if (e5.getText().toString().equals(""))
                    n5 = Integer.parseInt(e10.getText().toString());
                n6 = Integer.parseInt(e6.getText().toString());
                if (e6.getText().toString().equals(""))
                    n6 = Integer.parseInt(e10.getText().toString());
                n7 = Integer.parseInt(e7.getText().toString());
                if (e7.getText().toString().equals(""))
                    n7 = Integer.parseInt(e10.getText().toString());
                n8 = Integer.parseInt(e8.getText().toString());
                if (e8.getText().toString().equals(""))
                    n8 = Integer.parseInt(e10.getText().toString());
                n9 = Integer.parseInt(e9.getText().toString());
                if (e9.getText().toString().equals(""))
                    n9 = Integer.parseInt(e10.getText().toString());
                n10 = Integer.parseInt(e10.getText().toString());
                if (e10.getText().toString().equals(""))
                    n10 = Integer.parseInt(e10.getText().toString());
                try {
                    x = ((n1 - n2 - n3 - n4 - n5 - n6 - n7) / (double) (n8 + n9 + n10));
                } catch (Exception e) {
                    x = 00.00;
                }
                thesum = (x * 100);
                DecimalFormat form = new DecimalFormat("00.00");
                total = form.format(thesum) + "%";
                t1.setText(total);


            }
          }
        );
      }
    }

Hi jake jake, I just wanted to give you a heads up that I modified your post a bit just to fix the code display formatting.

thank you!! I have been trying to figure this out for a while

1 Answer

The problem is that you are trying to cast the string to an integer when the line is blank and does not have a value to convert:

// This line will cause an error if e1 is empty
 n1 = Integer.parseInt(e1.getText().toString()); 
 if (e1.getText().toString().isEmpty())
n1 = Integer.parseInt(e1.getText().toString());

// Corrected
if (!e1.getText().toString().isEmpty()) {
    n1 = Integer.parseInt(e1.getText().toString());
}