Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to resolve StringIndexOutOfBoundsException in android

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;
}
like image 877
Farhan Shah Avatar asked Apr 09 '14 07:04

Farhan Shah


People also ask

How do you resolve StringIndexOutOfBoundsException?

The StringIndexOutOfBoundsException is an exception in Java, and therefore can be handled using try-catch blocks using the following steps: Surround the statements that can throw an StringIndexOutOfBoundsException in try-catch blocks. Catch the StringIndexOutOfBoundsException.

How do I fix string 0 out of range?

To resolve the problem, simply remove any offending instances of unnecessary letters/symbols or words which do not belong in the mappings file.

What is StringIndexOutOfBoundsException?

java.lang.StringIndexOutOfBoundsException. Thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.


1 Answers

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()) : "";
like image 94
Kevin Robatel Avatar answered Sep 28 '22 06:09

Kevin Robatel