1
public void add(View v)
{

    EditText first=findViewById(R.id.first),second=findViewById(R.id.second);
    double f=Double.parseDouble(first.getText().toString());
    double s=Double.parseDouble(second.getText().toString());
    TextView result=findViewById(R.id.result);
    double r;
    if(TextUtils.isEmpty(first.getText().toString()))
    {
        first.setError("This field can't be empty");
    }
    else if(TextUtils.isEmpty(second.getText().toString()))
    {
        second.setError("This field can't be empty");
    }
    else {
        r = f + s;
        result.setText("" + r);
    }

}

I want to add two numbers from taking input from user and display an error msg if editText is empty.
But on executing this piece of code my app keeps crashing.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Vivank
  • 156
  • 1
  • 14

3 Answers3

3

You need convert your Editext value in to Double if Editext value is not empty

Try this

public void add(View v)
{

    EditText first=findViewById(R.id.first);
    EditText second=findViewById(R.id.second);      

    TextView result=findViewById(R.id.result);

    double r;

    if(TextUtils.isEmpty(first.getText().toString()))
    {
        first.setError("This field can't be empty");
    }
    else if(TextUtils.isEmpty(second.getText().toString()))
    {
        second.setError("This field can't be empty");
    }
    else {
        double s=Double.parseDouble(second.getText().toString());
        double f=Double.parseDouble(first.getText().toString());
        r = f + s;
        result.setText("" + r);
    }

}
Goku
  • 9,102
  • 8
  • 50
  • 81
  • 1
    Yeah it worked perfectly for me. Can you point out what's my mistake ? – Vivank Apr 23 '18 at 07:00
  • 1
    @Vivank the problem is if your `Editext value` is empty than your can't convert in tio a double and you will get number format exception – Goku Apr 23 '18 at 07:01
  • @Vivank please accept and upvote this ans if it help you – Goku Apr 23 '18 at 07:03
  • 1
    Yeah after 2 minutes @Prem . I was trying to do that from the beginning itself but it needs 10 minutes – Vivank Apr 23 '18 at 07:04
1
  1. Add "null" check ,before the empty check

eg :

if((first.gettext().toString) == null ||
    TextUtils.isEmpty(first.getText().toString()))
        {
            first.setError("This field can't be empty");
        }
        else if((second.gettext().toString) == null || TextUtils.isEmpty(second.getText().toString()))
        {
            second.setError("This field can't be empty");
        }
        else {
            r = f + s;
            result.setText("" + r);
        }
raj
  • 148
  • 13
1

Declare first,second globally

public void add(View v) {
    first = findViewById(R.id.first);
    second = findViewById(R.id.second);
    TextView result = findViewById(R.id.result);
    double r;
    if (Validates()) {
        double s = Double.parseDouble(second.getText().toString());
        double f = Double.parseDouble(first.getText().toString());
        r = f + s;
        result.setText("" + r);
    }
}


public boolean Validates() {
    if (first.getText().toString().equalsIgnoreCase("")) {
        first.setError("This field can't be empty");
        return false;
    } else if (second.getText().toString().equalsIgnoreCase("")) {
        second.setError("This field can't be empty");
        return false;
    } else {
        return true;
    }
}
GParekar
  • 1,209
  • 1
  • 8
  • 15