0

I have two EditText fields in an app. The first EditText is set up to accept strings, in my case a name:

     EditText nameField = (EditText) findViewById(R.id.nameOfCustomer);
     String name = nameField.getText().toString();

I have two EditText fields in an app. The first EditText is set up to accept strings, in my case a name:

    EditText nameField = (EditText) findViewById(R.id.nameOfCustomer);
    String name = nameField.getText().toString();

I then added the following java code for the first EditText to check for nulls:

    if(TextUtils.isEmpty(name)) 
    {
       Toast.makeText(this, "Please Enter Name  ",Toast.LENGTH_LONG).show();
       return;
    }

This works just fine and when the TextUtils.isEmpty is true it causes the toast to display if no entry is made in the first EditText field.

The second EditText has the input type set to android:inputType="number" since I want to have the user enter a number only

I have duplicated this same code for the second EditText:

   EditText nameField2 = (EditText) findViewById(R.id.ageOfPatient);
   String name2 = nameField2.getText().toString();

and I check it for nulls before I parse to an integer:

    if(TextUtils.isEmpty(name2)){
        Toast.makeText(this, "Please Enter Patient Age ",Toast.LENGTH_SHORT).show();
        return;
    }

    int val = Integer.parseInt( nameField2.getText().toString() );

However, it does not identify a null condition and causes the application to crash. I could not find anything that works.

Any suggestions would be most apprecited !

I then added the following java code for the first EditText to check for nulls:

  if(TextUtils.isEmpty(name)) {
       Toast.makeText(this, "Please Enter Name  ",Toast.LENGTH_LONG).show();
       return;
    }

This works just fine and when the TextUtils.isEmpty is true it causes the toast to display if no entry is made in the first EditText field.

The second EditText has the input type set to android:inputType="number" since I want to have the user enter a number only

I have duplicated this same code for the second EditText:

    EditText nameField2 = (EditText) findViewById(R.id.ageOfPatient);
    String name2 = nameField2.getText().toString();

and I check it for nulls before I parse to an integer:

    if(TextUtils.isEmpty(name2)){
        Toast.makeText(this, "Please Enter Patient Age ",Toast.LENGTH_SHORT).show();
        return;
    }

    int val = Integer.parseInt( nameField2.getText().toString() );

However, it does not identify a null condition and causes the application to crash. I could not find anything that works. Any suggestions would be most apprecited !

Komal12
  • 3,340
  • 4
  • 16
  • 25
  • 1
    Please add the log of the crash to your post. Have you tried debugging and checking the value of name2? – pablobu Mar 27 '17 at 01:31
  • I am embarrassed to say that I do not know how to do that yet. I will try to figure it out and post it if I can. Thanks for your help. Much appreciated ! –  Mar 27 '17 at 02:08

2 Answers2

0

I think you are looking for below code. If you will provide error log, it will be more clearer. Most likely you are submitting the form even after validation of the form is failed.

public class OPDForm extends AppCompatActivity implements View.OnClickListener {
    private EditText nameOfCustomer;
    private EditText ageOfPatient;
    private Button addButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_opd_form);
        nameOfCustomer = (EditText) findViewById(R.id.nameOfCustomer);
        ageOfPatient = (EditText) findViewById(R.id.ageOfPatient);
        addButton = (Button) findViewById(R.id.addButton);
        addButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == addButton) {
            addButton.requestFocus();
            if(isValidForm()) {
                //Submit Form Here
                //Most likely you are invoking below line even after validation fails, but now we are it will be called only when your form validation will get success
                int val = Integer.parseInt( ageOfPatient.getText().toString() );
            }
        }
    }

    private boolean isValidForm() {
        if(TextUtils.isEmpty(nameOfCustomer.getText().toString())) {
            Toast.makeText(this, "Please Enter Name  ", Toast.LENGTH_LONG).show();
            return false;
        } else if(TextUtils.isEmpty(ageOfPatient.getText().toString())) {
            Toast.makeText(this, "Please Enter Patient Age ", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }
}
Kamal Singh
  • 990
  • 8
  • 13
  • Thank you for taking the time to share your incredibly well crfted solution. I will look it over and see what I can learn from it. I m a rank novice so your help is appreciated ! –  Mar 27 '17 at 02:02
  • Most Welcome @Tom!! – Kamal Singh Mar 27 '17 at 02:06
0

I discovered that I had android:text for the second EditText instead of Android:hint so the null checker was seeing text instead of what I thought was the hint, duh. I stumbled across this when I deleted the text in test mode and found the code worked just fine, so I went back to compare the code on both EditText fields and sure enought, the second text had android:text instead of android:hint. Thanks to all who helped me out. As I stated, I am a novice, but quickly learning. Thanks again to all ! Tom