How to fix String Index Out of Range?

My code ran perfectly yesterday and I saved multiple backups at different stages, all of which worked yesterday. This is the specific snippet of code that worked yesterday but today is providing me with an error: Thanks in advance.

if ((!replaceThis.equals(null)) && !itsInDictionary) {
                        suggestions.setText(ReplaceAt(text, replaceThis, 
checkDictionary(replaceThis))); //this one is 183
                    }    

String ReplaceAt (String input, String search, String replace){
   //Log.d("Playing", "ReplaceAt is running");
   int start = input.lastIndexOf(search);
   int len = search.length();
   int end = start+len;
   String word = input.substring(0, start)//this one is 410
           + input.substring(start, end).replace (search, replace)
           + input.substring(end);
    return word;
        }

Errors:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.spellingsclient, PID: 5687
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
        at java.lang.String.substring(String.java:2036)
        at     com.example.spellingsclient.SpellingsClient.ReplaceAt(SpellingsClient.java:410)
        at     com.example.spellingsclient.SpellingsClient$2.onTextChanged(SpellingsClient.java:183)
        at android.widget.TextView.sendOnTextChanged(TextView.java:9754)
        at android.widget.TextView.handleTextChanged(TextView.java:9851)
        at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:12509)
        at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1263)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:575)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:506)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:36)
        at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:843)
        at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:197)
        at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
        at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:341)
        at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

1 answer

  • answered 2019-06-12 17:10 contaminate

    Solved it. When I installed a new virtual device, I forgot to turn off its autocorrect. The autocorrect was what was messing up my code. Makes sense since only incorrectly spelled user input was causing the crash. Works fine now that I turned it off.

    (Not sure what's up with the downvotes though. Did I format my question incorrectly? I tried. Sorry if I did.)