简体   繁体   中英

Validation for empty edittext in android

What i am doing: :

  • I have two edittext and i am trying to perform validation on them
  • condition i am checking is if both are not having nothing entered pop a dialog box

Code i am using for validation ::

private void chkUsrIpValidation() {
        if((edtTxtCity.getText().toString().trim().equals(""))&&(edtTxtDate.getText().toString().trim().equals(""))){
            DlgUniversalError.showCustomAlert(getActivity(), "Please enter the city name");
        }   
    }

Error i am facing ::

06-07 23:01:38.431: E/AndroidRuntime(915): FATAL EXCEPTION: main
06-07 23:01:38.431: E/AndroidRuntime(915): java.lang.NullPointerException
06-07 23:01:38.431: E/AndroidRuntime(915):  at com.findmybuffet.fragments.FrgMdSearch.chkUsrIpValidation(FrgMdSearch.java:126)
06-07 23:01:38.431: E/AndroidRuntime(915):  at com.findmybuffet.fragments.FrgMdSearch.access$0(FrgMdSearch.java:118)
06-07 23:01:38.431: E/AndroidRuntime(915):  at com.findmybuffet.fragments.FrgMdSearch$1.onClick(FrgMdSearch.java:110)
06-07 23:01:38.431: E/AndroidRuntime(915):  at android.view.View.performClick(View.java:3480)
06-07 23:01:38.431: E/AndroidRuntime(915):  at android.view.View$PerformClick.run(View.java:13983)
06-07 23:01:38.431: E/AndroidRuntime(915):  at android.os.Handler.handleCallback(Handler.java:605)
06-07 23:01:38.431: E/AndroidRuntime(915):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-07 23:01:38.431: E/AndroidRuntime(915):  at android.os.Looper.loop(Looper.java:137)
06-07 23:01:38.431: E/AndroidRuntime(915):  at android.app.ActivityThread.main(ActivityThread.java:4340)
06-07 23:01:38.431: E/AndroidRuntime(915):  at java.lang.reflect.Method.invokeNative(Native Method)
06-07 23:01:38.431: E/AndroidRuntime(915):  at java.lang.reflect.Method.invoke(Method.java:511)
06-07 23:01:38.431: E/AndroidRuntime(915):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-07 23:01:38.431: E/AndroidRuntime(915):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-07 23:01:38.431: E/AndroidRuntime(915):  at dalvik.system.NativeStart.main(Native Method)

How can i resolve this, Am i specifying && condition wrong ?

One of my EditTexts must be null. Those are edtTxtCity.getText() and edtTxtDate.getText() are the only two points in the if-statement that can cause an NPE.

It may also be easier to use TextUtils to check in which you got TextUtils.isEmpty(edtTxtCity.getText()) . It'll return true whether the text is null or empty.

if(null != edtTxtCity 
&& edtTxtCity.getText().toString().trim().length() > 0){
}

Why not the following,

private void chkUsrIpValidation() 
{
    if((edtTxtCity.getText() != null || edtTxtDate.getText() != null)
    {
        if(!"".equals(edtTxtCity.getText().toString()) || !"".equals(edtTxtDate.getText().toString())
        {
            DlgUniversalError.showCustomAlert(getActivity(), "Please enter the city name");
        }
    }   
}

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