bobbylight / RSyntaxTextArea

A syntax highlighting, code folding text editor for Java Swing applications.
BSD 3-Clause "New" or "Revised" License
1.12k stars 259 forks source link

NPE in SyntaxView.viewToModel #545

Closed ovna01 closed 4 months ago

ovna01 commented 6 months ago

Description In SyntaxView method viewToModel, the call to getTokenListForLine may return null and the following usage of the returned tokenList an NullPointerException. (line 975 and 977).

Steps to Reproduce We have got NPE in some cases, but have no way (yet) to reproduce this.

Expected behavior No NPE

Actual behavior NPE may occur

Java version Java 17

Additional context Possible solution - add a null check before calling tokenList.getListOffset

bobbylight commented 5 months ago

@ovna01 - getTokenList() should never return null. are you seeing this with a particular TokenMaker?

ovna01 commented 5 months ago

In RSyntaxDocument.getTokenListForLine (line 427) is null returned if getText throws BadLocationException. We have an own token maker, but we have not changed this.

bobbylight commented 5 months ago

@ovna01 can you provide the staack traace you got from the BadLocationException? I want to see if that's a root cause. Honestly I'm more inclined to return an empty new TokenImpl() than null to keep the contract non-null.

ovna01 commented 5 months ago

I guess that returning a new TokenImpl() would be correct. Below is the stack trace: java.lang.NullPointerException: Cannot invoke "org.fife.ui.rsyntaxtextarea.Token.getListOffset(org.fife.ui.rsyntaxtextarea.RSyntaxTextArea, javax.swing.text.TabExpander, float, float)" because "tokenList" is null at org.fife.ui.rsyntaxtextarea.SyntaxView.viewToModel(SyntaxView.java:977) at java.desktop/javax.swing.plaf.basic.BasicTextUI$RootView.viewToModel(BasicTextUI.java:1656) at java.desktop/javax.swing.plaf.basic.BasicTextUI.viewToModel(BasicTextUI.java:1183) at java.desktop/javax.swing.plaf.basic.BasicTextUI.viewToModel(BasicTextUI.java:1163) at java.desktop/javax.swing.text.DefaultCaret.positionCaret(DefaultCaret.java:341) at java.desktop/javax.swing.text.DefaultCaret.adjustCaret(DefaultCaret.java:557) at java.desktop/javax.swing.text.DefaultCaret.adjustCaretAndFocus(DefaultCaret.java:544) at java.desktop/javax.swing.text.DefaultCaret.mousePressed(DefaultCaret.java:534) at org.fife.ui.rtextarea.ConfigurableCaret.mousePressed(ConfigurableCaret.java:380) at com.onseven.dbvis.editor.rsyntax.multiselect.DbVisMultiCaret.mousePressed(java.awt.event.MouseEvent)(DbVisMultiCaret.java:196) at java.desktop/java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:288) at java.desktop/java.awt.Component.processMouseEvent(Component.java:6623) at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389) at org.fife.ui.rtextarea.RTextArea.processMouseEvent(RTextArea.java:1060) at java.desktop/java.awt.Component.processEvent(Component.java:6391) at java.desktop/java.awt.Container.processEvent(Container.java:2266) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833) at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948) at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4572) at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775)