toString() methods should not throw an exception under any circumstances (I think it is when the (x,y) coordinates are outside of the Text bounds). The second invocation of toString on the same instance does not cause NPE, and the insertionIndex field gets set in getInsertionIndex() just before the exception is thrown - so not only we have an exception, but also the object might enter an inconsistent/incorrect state.
This may be a sign of a larger issue, but at least toString() should print N/A if HitInfo.getInsertionIndex() throws an exception.
```
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: offset out of bounds
at java.base/sun.text.RuleBasedBreakIterator.checkOffset(RuleBasedBreakIterator.java:730)
at java.base/sun.text.RuleBasedBreakIterator.following(RuleBasedBreakIterator.java:744)
at javafx.graphics/javafx.scene.text.HitInfo.getInsertionIndex(HitInfo.java:84)
at javafx.graphics/javafx.scene.text.HitInfo.toString(HitInfo.java:100)
at java.base/java.lang.String.valueOf(String.java:4225)
at java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
at andy_test/goryachev.rich.RichTextAreaBehavior.nextCharacterVisually_textArea(RichTextAreaBehavior.java:506)
at andy_test/goryachev.rich.RichTextAreaBehavior.nextCharacterVisually(RichTextAreaBehavior.java:389)
at andy_test/goryachev.rich.RichTextAreaBehavior.moveLeft(RichTextAreaBehavior.java:316)
at andy_test/goryachev.rich.RichTextAreaBehavior.handleKeyEvent(RichTextAreaBehavior.java:151)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
```
This may be a sign of a larger issue, but at least toString() should print N/A if HitInfo.getInsertionIndex() throws an exception.
```
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: offset out of bounds
at java.base/sun.text.RuleBasedBreakIterator.checkOffset(RuleBasedBreakIterator.java:730)
at java.base/sun.text.RuleBasedBreakIterator.following(RuleBasedBreakIterator.java:744)
at javafx.graphics/javafx.scene.text.HitInfo.getInsertionIndex(HitInfo.java:84)
at javafx.graphics/javafx.scene.text.HitInfo.toString(HitInfo.java:100)
at java.base/java.lang.String.valueOf(String.java:4225)
at java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
at andy_test/goryachev.rich.RichTextAreaBehavior.nextCharacterVisually_textArea(RichTextAreaBehavior.java:506)
at andy_test/goryachev.rich.RichTextAreaBehavior.nextCharacterVisually(RichTextAreaBehavior.java:389)
at andy_test/goryachev.rich.RichTextAreaBehavior.moveLeft(RichTextAreaBehavior.java:316)
at andy_test/goryachev.rich.RichTextAreaBehavior.handleKeyEvent(RichTextAreaBehavior.java:151)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
```
- relates to
-
JDK-8194704 Text/TextFlow hitTest() javadoc
- Resolved
-
JDK-8304831 TextFlow.hitTest.insertionIndex incorrect with surrogate pairs
- Resolved