Search code examples
javajavafxindexoutofboundsexceptiongluon

StringIndexOutOfBoundException occurs when typing anything into a JavaFX TextField in both JDK21 and JDK8, Windows11


When I run this simple code snippet of a JavaFX TextField element, I type something into the text field, and then StringIndexOutOfBoundsException is thrown periodically.

versions

JDK: 21.0.0, 21.0.2, 1.8
JavaFX: 
`javafx.runtime.version=8.0.65 javafx.runtime.build=b17`
`javafx.version=21 javafx.runtime.version=21+31 javafx.runtime.build=31`
Windows:
`Edition=Windows 11 Pro, Version=23H2`

Error Message

Exception in thread "JavaFX Application Thread" java.lang.StringIndexOutOfBoundsException: Range [1, -2147483648) out of bounds for length 1
    at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
    at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
    at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
    at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
    at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
    at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4861)
    at java.base/java.lang.String.substring(String.java:2830)
    at javafx.graphics@21/com.sun.glass.ui.win.WinTextRangeProvider.GetText(WinTextRangeProvider.java:367)
    at javafx.graphics@21/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@21/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
    at java.base/java.lang.Thread.run(Thread.java:1583)

source code

package comp3111.qsproject;

// Java program to create a textfield and add it to stage
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TextFieldTest extends Application {

    // launch the application
    public void start(Stage s)
    {
        // set title for the stage
        s.setTitle("creating TextField");

        // create a textfield
        TextField b = new TextField();

        // create a stack pane
        StackPane r = new StackPane();

        // add textfield
        r.getChildren().add(b);

        // create a scene
        Scene sc = new Scene(r, 200, 200);

        // set the scene
        s.setScene(sc);

        s.show();
    }

    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}

This problem was not resolved when I reinstalled my JDK.


Solution

  • Workaround: Close other running apps.

    Update: There is an official issue for this: https://bugs.openjdk.org/browse/JDK-8330462.

    At JabRef#11151 it was reported that the DeepL Windows App caused the issue. I tried it on my Windows 10 machine. Having DeepL running: Error appears. DeepL closed: Error gone.


    For the others to reproduce:

    1. Go to https://www.deepl.com/en/app/
    2. Download the app
    3. Install the app for the current user
    4. Start the app
    5. Mark some text
    6. Press Ctrl+C+C to check that DeepL really runs
    7. Switch to a JavaFX appp
    8. Enter something in a text field
    9. You should see the exception

    If you could reproduce (or not), please share details at https://github.com/koppor/jfx/pull/2. It seems that not all persons can reproduce and there could be some specific setups. - I personally fired up a fresh Windows on Azure, created another user login (without (!) admin rights), logged in with that user and could reproduce. The issue does not appear if logged in as administrator!