简体   繁体   中英

Empty EditText crash Android

I just started learning Android and I made a code that gives you an equation and you need to solve it, it's very simple but if someone presses the check button and the edit text is empty the app crashes and I want it to count it as mistake.. here is my code :

   public void Generate(View v) {
    x1=10+(int)((99-10+1)*Math.random());
    x2=10+(int)((99-10+1)*Math.random());
    tv1.setText("" + x1 + "+" + x2 + "=" + "?");
}

public void Check(View view) {
    answer1 = et1.getText().toString();
    answer = Integer.parseInt(answer1);
    if (answer == (x1 + x2)) {
        count1++;
        count2++;
        Toast.makeText(getApplicationContext(), "Correct !",
                Toast.LENGTH_SHORT).show();
    }


    if(answer!=(x1+x2))
    {
        count1++;
        count3++;
        Toast.makeText(getApplicationContext(), "Wrong !",
                Toast.LENGTH_SHORT).show();
    }

    if(answer1=="")
    {
        count1++;
        count3++;
        Toast.makeText(getApplicationContext(), "Don't try to cheat !",
                Toast.LENGTH_SHORT).show();
    }

    et1.setText("");
    tv2.setText("Number of questions : "+count1);
    tv3.setText("Right answers : "+count2);
    tv4.setText("Wrong answers : "+count3);

    Generate(view);
}

The thing is that you are converting et1.getText() to String and if this returns null value, null pointer exception is raised. so need to implement a check whether the value is null or not :

if(et1.getText()!=null){

answer1 = et1.getText().toString();
if(et1.getText().toString().length()>0){
    answer = Integer.parseInt(answer1);
}
    if (answer == (x1 + x2)) {
        count1++;
        count2++;
        Toast.makeText(getApplicationContext(), "Correct !",
                Toast.LENGTH_SHORT).show();
    }


    if(answer!=(x1+x2))
    {
        count1++;
        count3++;
        Toast.makeText(getApplicationContext(), "Wrong !",
                Toast.LENGTH_SHORT).show();
    }

    if(answer1=="")
    {
        count1++;
        count3++;
        Toast.makeText(getApplicationContext(), "Don't try to cheat !",
                Toast.LENGTH_SHORT).show();
    }

    et1.setText("");
    tv2.setText("Number of questions : "+count1);
    tv3.setText("Right answers : "+count2);
    tv4.setText("Wrong answers : "+count3);

    Generate(view);
}

Another thing is that if String is empty then it will be unable to parse to int so you also need check for that

either

if(et1.getText().toString().length()>0){
        answer = Integer.parseInt(answer1);
    }

or

if(!TextUtils.isEmpty(answer1)){
answer = Integer.parseInt(answer1);
}

You can check if the EditText is empty . Do it as below, in your code

if (!et1.getText().toString().matches(""))
{
    answer1 = et1.getText().toString();
}
else
{
    //No value entered
}

OR simply

if (!et1.getText().toString().equals(""))
{
    answer1 = et1.getText().toString();
}
else
{
    //No value entered
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM