Skip to content

[BUG] java.lang.IndexOutOfBoundsException: Index 9 out of bounds for length 9 #10178

Closed
@kirillzyusko

Description

@kirillzyusko

Summary

App crashes when using react-native SDK + react-native-keyboard-controller.

Not sure why, but it seems like crash happens if you add addTextChangedListener to expiration date field 🤷‍♂ In this code: https://github.com/kirillzyusko/react-native-keyboard-controller/blob/f4eb08879e6c8f346d5a1c3935227252c4e96e20/android/src/main/java/com/reactnativekeyboardcontroller/extensions/EditText.kt#L24-L92

The try block produces IllegalArgumentException(because ReactEditText is not castable to Stripe input), so I simply call addTextChangedListener (I'm adding a listener when text-field gains a focus). When you finish entering expiration date then app instantly crashes with:

ERROR  Your app just crashed. See the error below.
java.lang.IndexOutOfBoundsException: Index 9 out of bounds for length 9
  jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
  jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
  jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)

I thought that maybe I'm dispatching a react event and because of that it breaks something, but turns out that if I add even empty handler, then exception still happens:

val listener =
    object : TextWatcher {
      @Suppress("detekt:EmptyFunctionBlock")
      override fun afterTextChanged(s: Editable?) = Unit

      @Suppress("detekt:EmptyFunctionBlock")
      override fun beforeTextChanged(
        s: CharSequence?,
        start: Int,
        count: Int,
        after: Int,
      ) = Unit

      override fun onTextChanged(
        s: CharSequence?,
        start: Int,
        before: Int,
        count: Int,
      ) {
        val currentText = s.toString()

        if (currentText != lastText) {
          lastText = currentText
          // action(currentText)
        }
      }

Code to reproduce

You can use this example: kirillzyusko/react-native-keyboard-controller#808

Field is located in AwareScrollView example.

Android version

Tested on API 35

Impacted devices

I believe all devices are impacted, but I tested only on simulator with API 35.

Installation method

As react-native SDK.

Dependency Versions

kotlin:
stripe-android:
Android Gradle Plugin:

Gradle:

Build time: 2024-09-23 21:28:39 UTC
Revision: 415adb9e06a516c44b391edff552fd42139443f7

Kotlin: 1.9.24
Groovy: 3.0.22
Ant: Apache Ant(TM) version 1.10.14 compiled on August 16 2023
Launcher JVM: 17.0.9 (Microsoft 17.0.9+8-LTS)
Daemon JVM: /Users/kirylziusko/.sdkman/candidates/java/17.0.9-ms (no JDK specified, using current Java home)
OS: Mac OS X 15.2 aarch64

SDK classes

🤷‍♂

Video

Screen.Recording.2025-02-14.at.16.29.37.mov

Other information

Maybe it's react-native SDK issue - if so, then can you transfer the issue to the corresponding repository?

Stacktrace

2025-02-14 16:29:39.342 22632-22632 AndroidRuntime          com...reactnativekeyboardcontroller  E  FATAL EXCEPTION: main (Ask Gemini)
                                                                                                    Process: com.example.reactnativekeyboardcontroller, PID: 22632
                                                                                                    java.lang.IndexOutOfBoundsException: Index 9 out of bounds for length 9
                                                                                                    	at jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
                                                                                                    	at jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
                                                                                                    	at jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
                                                                                                    	at java.util.Objects.checkIndex(Objects.java:359)
                                                                                                    	at java.util.ArrayList.get(ArrayList.java:434)
                                                                                                    	at android.widget.TextView.sendAfterTextChanged(TextView.java:12745)
                                                                                                    	at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:16334)
                                                                                                    	at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1278)
                                                                                                    	at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:578)
                                                                                                    	at androidx.emoji2.text.SpannableBuilder.replace(SpannableBuilder.java:315)
                                                                                                    	at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:508)
                                                                                                    	at androidx.emoji2.text.SpannableBuilder.replace(SpannableBuilder.java:305)
                                                                                                    	at androidx.emoji2.text.SpannableBuilder.replace(SpannableBuilder.java:49)
                                                                                                    	at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:129)
                                                                                                    	at android.widget.TextView.doKeyDown(TextView.java:9814)
                                                                                                    	at android.widget.TextView.onKeyDown(TextView.java:9569)
                                                                                                    	at android.view.KeyEvent.dispatch(KeyEvent.java:2966)
                                                                                                    	at android.view.View.dispatchKeyEvent(View.java:16379)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:513)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at com.facebook.react.ReactRootView.dispatchKeyEvent(ReactRootView.java:328)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1967)
                                                                                                    	at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:472)
                                                                                                    	at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1955)
                                                                                                    	at android.app.Activity.dispatchKeyEvent(Activity.java:4479)
2025-02-14 16:29:39.343 22632-22632 AndroidRuntime          com...reactnativekeyboardcontroller  E  	at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.kt:103) (Ask Gemini)
                                                                                                    	at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:85)
                                                                                                    	at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.kt:117)
                                                                                                    	at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:604)
                                                                                                    	at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:60)
                                                                                                    	at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:3413)
                                                                                                    	at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:386)
                                                                                                    	at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:7714)
                                                                                                    	at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:7554)
                                                                                                    	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6955)
                                                                                                    	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:7012)
                                                                                                    	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6978)
                                                                                                    	at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:7144)
                                                                                                    	at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6986)
                                                                                                    	at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:7201)
                                                                                                    	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6959)
                                                                                                    	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:7012)
                                                                                                    	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6978)
                                                                                                    	at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6986)
                                                                                                    	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6959)
                                                                                                    	at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:10080)
                                                                                                    	at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:10031)
                                                                                                    	at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:10000)
                                                                                                    	at android.view.ViewRootImpl$ViewRootHandler.handleMessageImpl(ViewRootImpl.java:6674)
                                                                                                    	at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:6540)
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:107)
                                                                                                    	at android.os.Looper.loopOnce(Looper.java:232)
                                                                                                    	at android.os.Looper.loop(Looper.java:317)
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:8699)
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions