-1
 AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Add new product");
        builder.setView(subView);
        builder.create();

        builder.setPositiveButton("ADD PRODUCT", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                final String name = nameField.getText().toString();
                final int quantity = Integer.parseInt(quantityField.getText().toString());


                if(TextUtils.isEmpty(name) || quantity <= 0)
                {
                    Toast.makeText(MainActivity.this, "Something went wrong. Check your input values", Toast.LENGTH_LONG).show();
                }
                else{
                    Product newProduct = new Product(name, quantity);
                    mDatabase.addProduct(newProduct);

                    //refresh the activity
                    finish();
                    startActivity(getIntent());
                }

            }
        });

This is my code where the product is the database and there are two fields in it: name and quantity. Now when I leave the fields empty the application crashes.

I have even tried like:

if(nameFields.getText().tostring().isEmpty() ||quantityFields.getText.toString.isEmpty())
{
//Toast for error
}

If I enter a single field in the alert builder, i.e. when I enter only the name it accepts the value which it should not. Instead, it must pop an alert for not entering all required values. How can I set up this alert?

This is the error I get:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.inducesmile.androidsqliteexample, PID: 15729
                  java.lang.NumberFormatException: For input string: ""
                      at java.lang.Integer.parseInt(Integer.java:533)
                      at java.lang.Integer.parseInt(Integer.java:556)
                      at com.inducesmile.androidsqliteexample.MainActivity$2.onClick(MainActivity.java:80)
                      at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:163)
                      at android.app.ActivityThread.main(ActivityThread.java:6238)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Swagnash Thor
  • 19
  • 1
  • 10
  • 1
    Please post your error and subView.xml file code Or you can check like this for the field is empty or not name.matches("") – Md Mobinur Rahman Mar 18 '19 at 05:48
  • AndroidRuntime: FATAL EXCEPTION: main Process: com.inducesmile.androidsqliteexample, PID: 13147 java.lang.NumberFormatException: For input string: "" at java.lang.Integer.parseInt(Integer.java:533) at java.lang.Integer.parseInt(Integer.java:556) – Swagnash Thor Mar 18 '19 at 06:17
  • The problem is in the following line `final int quantity = Integer.parseInt(quantityField.getText().toString());`. You're trying to convert an empty string `""` to an integer. Which naturally will raise an exception. You need to check if your String is a correct integer. Please take a look https://stackoverflow.com/questions/237159/whats-the-best-way-to-check-if-a-string-represents-an-integer-in-java fore more details about converting string to integer. – ישו אוהב אותך Mar 18 '19 at 07:00
  • 1
    Duplicate of https://stackoverflow.com/questions/39849984/what-is-a-numberformatexception-and-how-can-i-fix-it – Zoe Mar 18 '19 at 07:04

2 Answers2

0

please add this line to bottom of codes

AlertDialog alert = builder.create();
alert.show();
mmfarzaneh
  • 381
  • 1
  • 11
0

The issue is here:

final int quantity = Integer.parseInt(quantityField.getText().toString());

You are doing this no matter what, even if quantityField is empty. You only check if it is empty after attempting to parse it to an Integer. The error is telling you that.

java.lang.NumberFormatException: For input string: ""

So perhaps rewrite your onClick function to be the following:

final String name = nameField.getText().toString();
final String quantityStr = quantityField.getText().toString();

if(TextUtils.isEmpty(name) || TextUtils.isEmpty(quantityStr))
{
     Toast.makeText(MainActivity.this, "Something went wrong. Check your input values", Toast.LENGTH_LONG).show();
}
else {
    final int quantity = Integer.parseInt(quantityStr);
    // TODO: here you can check if quantity <= 0 and handle that appropriately
    Product newProduct = new Product(name, quantity);
    mDatabase.addProduct(newProduct);

    //refresh the activity
    finish();
    startActivity(getIntent());
}

What I changed:

  • first created a string quantityStr
  • show an error if it's empty, just like you did for the name field
  • parse it after it is confirmed to not be empty
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97