14

This is my logcat report:

java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=2
at java.lang.String.startEndAndLength(String.java:583)
at java.lang.String.substring(String.java:1464)
at com.buzzador.profile.getValidPhoneNumber(profile.java:1966)
at com.buzzador.profile.setDataForServer(profile.java:1717)
at com.buzzador.profile$5.onClick(profile.java:236)
at android.view.View.performClick(View.java:4377)
at android.view.View$PerformClick.run(View.java:18044)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5306)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

and i think the issue is in this function:

public String getValidPhoneNumber (String phoneNumber, String country)
{
    String csValidPhoneNumber = "";

    phoneNumber = getPhoneNumberWithoutReqularExpresions(phoneNumber);
    phoneNumber = phoneNumber.replaceFirst ("^0*", "");

    String csCountryCode = getCountryCode(country);

    String csAppendedCode = phoneNumber.substring(0, csCountryCode.length());
    if(csAppendedCode.equals(csCountryCode))
    {
        csValidPhoneNumber = "+" + phoneNumber;
        return csValidPhoneNumber;
    }

    csValidPhoneNumber = "+" + csCountryCode + phoneNumber;

    return csValidPhoneNumber;
}
Farhan Shah
  • 2,344
  • 7
  • 28
  • 54

2 Answers2

14

Are you sure that phoneNumber is not equal to ""? You have to check that phoneNumber have more chars than the csCountryCode.length().

String csAppendedCode = phoneNumber.length() > csCountryCode.length() ? phoneNumber.substring(0, csCountryCode.length()) : "";
Kevin Robatel
  • 8,025
  • 3
  • 44
  • 57
1

The last index of a String is string.length()-1 but you are trying to access index with string.length() which doesn't exist, that's why you are getting this exception...

java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=2

Now, you should change this line...

String csAppendedCode = phoneNumber.substring(0, csCountryCode.length());

to this...

String csAppendedCode = phoneNumber.substring(0, csCountryCode.length()-1);
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • Not a valid answer always!!! what if `csCountryCode` length is greater than `phoneNumber` length? – Gopal Gopi Apr 09 '14 at 07:18
  • the `length()` always returns `lastindex+1` value, so when you will try to access last index of a `String` with `lastIndex+1` then you will always get `StringIndexOutOfBoundsException`. – Hamid Shatu Apr 09 '14 at 07:28
  • 1
    Once try this `String string = "Android"; System.out.println(string.substring(0, string.length()));`... Does not throw any Exception... – Gopal Gopi Apr 09 '14 at 07:30
  • 1
    @HamidShatu No. Read the java APIs: *substring(int start, int end): Returns a new string that is a substring of this string. The substring* ***begins at the specified beginIndex and extends to the character at index endIndex - 1***. Your answer is wrong. – BackSlash Apr 09 '14 at 07:34