0

How is it possible to prevent my app from crashing when my EditText field is empty?

    public class Credits extends Activity {
    int inaugural = 1992;
    int differenceInYears;
    int userGuess;
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int output;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credits);
        final EditText Guess=(EditText)findViewById(R.id.txtYearGuess);
        Button go = (Button)findViewById(R.id.btnCalculate);
        final TextView result = ((TextView)findViewById(R.id.txtResult));

        go.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int userGuess= Integer.parseInt(Guess.getText().toString());
                differenceInYears = year - inaugural;
                output = userGuess - differenceInYears;

                if (output < -1) {
                    result.setText("Guess again! You guessed too low!");

                    }

                else if (output == 1) {
                    result.setText("You're REALLY close! You guessed too high!");


                    }

                else if (output == -1) {
                    result.setText("You're REALLY close! You guessed too low!");


                    }

                else if (output > 1) {
                    result.setText("Guess again! You guessed too high!");


                    }

                else {
                    result.setText("Good job! You're an FRC Genious!");

                    }



            }

        });
    }

}

Is it as simple as having another if statement watching for an "empty" variable? If so, what would the code for that look like? I'm sort of at a loss for ideas in preventing this crash from happening. If there is any sort of report from Eclipse that could help answer this question, please let me know where to find it.

Thanks!

EDIT

I tried adding the recommended TextUtils but they didn't solve the app crash.

public class Credits extends Activity {
    int inaugural = 1992;
    int differenceInYears;
    int userGuess;
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int output;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credits);
        final EditText Guess=(EditText)findViewById(R.id.txtYearGuess);
        Button go = (Button)findViewById(R.id.btnCalculate);
        final TextView result = ((TextView)findViewById(R.id.txtResult));

        go.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int userGuess= Integer.parseInt(Guess.getText().toString());
                differenceInYears = year - inaugural;
                output = userGuess - differenceInYears;

                if(!TextUtils.isEmpty(Guess.getText().toString())) {
                    Toast.makeText(Credits.this, "Please input your Guess", Toast.LENGTH_LONG).show();
                    }

                else {

                if (output < -1) {
                    result.setText("Guess again! You guessed too low!");

                    }

                else if (output == 1) {
                    result.setText("You're REALLY close! You guessed too high!");


                    }

                else if (output == -1) {
                    result.setText("You're REALLY close! You guessed too low!");


                    }

                else if (output > 1) {
                    result.setText("Guess again! You guessed too high!");


                    }

                else {
                    result.setText("Good job! You're an FRC Genious!");

                    }

            }

            }

        });
    }

}
tolgap
  • 9,629
  • 10
  • 51
  • 65
Joseph
  • 3
  • 3

2 Answers2

0

In onClick method of onClickListener add the check

    String text = Guess.getText().toString();
    if(text != null && !text.trim().equals("")){
       try{
            //Do watever you want to do here
       }catch(NumberFormatException e){
            e.printStackTrace();
       }
    }

You are also doing int parsing so you can also check if TextUtils.isDigitsOnly (Doc)

Manish
  • 1,215
  • 10
  • 29
0

The getText().getString() is guaranteed to never return null so long as your edittext is properly initialized (ie it points to actual view), so at worst you'd get an empty string --> "". To check for that, do something along the lines of

if (!Guess.getText().getString().equals("")) If the acquired value does NOT equal "", then do stuff.

You may also want to wrap your parsing statement in a try-catch block to prevent instances where people enter in letters or something.

public static Integer tryParse(String text) {
   try {
     return new Integer(text);
   } catch (NumberFormatException e) {
     return null;
   }
}

Source for the try/parse: here

Community
  • 1
  • 1
Matter Cat
  • 1,538
  • 1
  • 14
  • 23