Waqas Aamer
Waqas Aamer

Reputation: 59

Android: whenever a edittext field is left empty, program closes giving exception

program sudenly close when any edittext is left empty when ever i pressed a button to move to next to activity. here is the code.

package cme.ws.com.ws.cme;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
* Created by Waqas Aamer on 5/27/2015.
*/
public class areaActivity extends Activity {

int plotarea, coveredarea;
int brickprice, blockprice, cementprice, sandprice, crushprice, ironprice;
int brickprice1, blockprice1, cementprice1, sandprice1, crushprice1, ironprice1;
int plotarea1, coveredarea1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_area);

    {
        final Context context = getApplicationContext();
        final int duration = Toast.LENGTH_SHORT;

        Button movenextarea=(Button) findViewById(R.id.buttonnextexteriorwall);
        movenextarea.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent intent = getIntent();
                if (null != intent)
                {
                    // test = intent.getStringExtra("NameOfVariable");
                    brickprice1 = intent.getIntExtra("anything00", brickprice);
                    blockprice1 = intent.getIntExtra("anything11", blockprice);
                    sandprice1 = intent.getIntExtra("anything22", sandprice);
                    ironprice1 = intent.getIntExtra("anything33", ironprice);
                    cementprice1 = intent.getIntExtra("anything44", cementprice);
                    crushprice1 = intent.getIntExtra("anything55", crushprice);
                }
                EditText areanumber1 = (EditText) findViewById(R.id.editTextplotarea);
                if(areanumber1 == null)
                {
                    Toast toast = Toast.makeText(context, "Please fill the plot area field ", duration);
                    toast.show();
                }
                else
                {
                    plotarea1 = Integer.parseInt(areanumber1.getText().toString());
                }

                EditText areanumber2 = (EditText) findViewById(R.id.editTextcoveredarea);
                if(areanumber2 == null)
                {
                    Toast toast1 = Toast.makeText(context, "Please fill the covered area field ", duration);
                    toast1.show();
                }
                else
                {
                    coveredarea1 = Integer.parseInt(areanumber2.getText().toString());
                }


                int sqrt = (int) Math.sqrt(plotarea1);
                int oneside = sqrt;

                Intent secondActivity = new Intent (getApplicationContext(), exteriorwallActivity.class);
                secondActivity.putExtra("anything000", brickprice1);
                secondActivity.putExtra("anything111", blockprice1);
                secondActivity.putExtra("anything222", cementprice1);
                secondActivity.putExtra("anything333", sandprice1);
                secondActivity.putExtra("anything444", ironprice1);
                secondActivity.putExtra("anything555", crushprice1);
                secondActivity.putExtra("anything", oneside);
                secondActivity.putExtra("anything1", coveredarea1);
                startActivity(secondActivity);
            }

        });




    }
}

}

i want this that whenver a edittext box is left empty .the variable in whcih it is storing that value automatically stores 0. eroor log is

07-05 23:20:42.206  14348-14348/cme.ws.com.ws.cme E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NumberFormatException: unable to parse '' as integer
            at java.lang.Integer.parseInt(Integer.java:412)
            at java.lang.Integer.parseInt(Integer.java:382)
            at cme.ws.com.ws.cme.areaActivity$1.onClick(areaActivity.java:52)
            at android.view.View.performClick(View.java:2408)
            at android.view.View$PerformClick.run(View.java:8816)
            at android.os.Handler.handleCallback(Handler.java:587)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:4627)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:521)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
            at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 338

Answers (2)

Alexander Goncharenko
Alexander Goncharenko

Reputation: 954

Your EditText fields cannot be equals null because in previous line of your verification you have initialized them. Instead of using if (areanumber1 == null) and if (areanumber2 == null), check your EditText like this:

boolean isEmpty(EditText textField) {
    return textField.getText().toString().trim().length() == 0;
}

If function return false means that EditText is not empty and return true means that EditText is empty.

Upvotes: 1

Karakuri
Karakuri

Reputation: 38585

This test...

if(areanumber1 == null)

does not check if the EditText called areanumber1 has no text. This checks if the variable named areanumber1 is a null reference. Hopefully it's not since you know what IDs your layout XML is using. What's happening is your code is trying to use parseInt() on an empty string.

You should instead do something like this:

String area1text = areanumber1.getText().toString(); // works even if empty
if (TextUtils.isEmpty(area1Text)) { // text is empty
    plotarea1 = 0;
} else {
    plotarea1 = Integer.parseInt(area1text);
}

TextUtils is an Android class with some useful static methods like the one I used above. Also, make sure your EditTexts are using the proper android:inputType to restrict the text to numbers only, or else the parseInt() will throw an exception.

Upvotes: 0

Related Questions