Rahul
Rahul

Reputation: 404

App Crash While Clicking Button When EditText is Null

Here validating my mobile and password data,but when clicking button when EditText null My app crash!

This is what I have tried:

String mobile = edtMobile.getText().toString();
            String password = edtPassword.getText().toString();
            String phone = String.valueOf(mobile);
            char c  = phone.charAt(0);

                if (mobile.length() > 0&& mobile.length() == 10) {
                    if (phone.length()> 0  && c == '8' || c == '9' || c == '7') {
                        if (password.length() > 0 && password.length() < 4 && password.length() > 15) {
                        } else {
                            Toast.makeText(AuthenticationActivity.this, "Password Character Must Be Between 4 - 15", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(AuthenticationActivity.this, "Number Must Begin with 9 8 7", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } else {
                    Toast.makeText(AuthenticationActivity.this, "Enter Only 10 Digit Number", Toast.LENGTH_SHORT).show();
                    return;
                }

my error given below

java.lang.StringIndexOutOfBoundsException: length=0; index=0 at java.lang.String.indexAndLength(String.java:500) at java.lang.String.charAt(String.java:494) at com.example.rahul.logindatabasetask.Activity.AuthenticationActivity$1.onClick(AuthenticationActivity.java:42) at android.view.View.performClick(View.java:4780) at android.view.View$PerformClick.run(View.java:19866) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Upvotes: 0

Views: 401

Answers (4)

mohammadrsh1
mohammadrsh1

Reputation: 87

check it is empty or not.

 edt.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.i("TESTTEST","" + s);
            if (s.length() !=0){
//                  do sth
            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

Upvotes: 0

Apoorv Karkare
Apoorv Karkare

Reputation: 89

Reason for 'IndexOutOfBounds' exception

This exception comes when you try to get the value of the index that is not really exist in the bounds(In your case length of 'phone' String which is null). So when you try to get the value at index '0' it throws the exception.

Solution

Always put a null check and 'Empty String' check before accessing any string value. Just do the following:

if(phone!=null && !phone.isEmpty()) {
    char c  = phone.charAt(0);
}

Upvotes: 3

Bhargav Thanki
Bhargav Thanki

Reputation: 4954

Check if phone string is empty or not before getting charAt

if(!phone.isEmpty()) {
    char c  = phone.charAt(0);
}

Upvotes: 0

Ravi
Ravi

Reputation: 35549

put this line char c = phone.charAt(0); after if (mobile.length() > 0&& mobile.length() == 10)

It is giving error because phone is blank and you are trying to get character at first position.

Upvotes: 2

Related Questions