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

Possible NPE in SyntaxView.calculateLongestLine #485

Closed pskowronek closed 1 year ago

pskowronek commented 1 year ago

Description Possible NPE in SyntaxView.calculateLongestLine.

Steps to Reproduce Not known yet.

Expected behavior No NPE thrown.

Actual behavior NPE is thrown

Additional context

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.awt.Component.getFont()" because "c" is null
        at org.fife.ui.rsyntaxtextarea.SyntaxView.calculateLongestLine(SyntaxView.java:103)
        at org.fife.ui.rsyntaxtextarea.SyntaxView.updateMetrics(SyntaxView.java:904)
        at org.fife.ui.rsyntaxtextarea.SyntaxView.setSize(SyntaxView.java:812)
        at java.desktop/javax.swing.plaf.basic.BasicTextUI$RootView.setSize(BasicTextUI.java:1823)
        at java.desktop/javax.swing.plaf.basic.BasicTextUI.getPreferredSize(BasicTextUI.java:948)
        at java.desktop/javax.swing.plaf.basic.BasicTextAreaUI.getPreferredSize(BasicTextAreaUI.java:121)
        at java.desktop/javax.swing.JComponent.getPreferredSize(JComponent.java:1734)
        at java.desktop/javax.swing.JTextArea.getPreferredSize(JTextArea.java:623)
        at com.mucommander.viewer.text.TextLineNumbersPanel.lambda$documentChanged$0(TextLineNumbersPanel.java:314)
        at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Reported in another project: https://github.com/mucommander/mucommander/issues/841 - of course something might be wrong in muC, but nevertheless SyntaxView.calculateLongestLine could check if c is not null, especially that View.getContainer() states that it may return null.

bobbylight commented 1 year ago

Interesting - even javax.swing.text.PlainView, which SyntaxView cribs off of, assumes getContainer() is never null. I think it should be the case that the conatiner is never null if that method is called, but perhaps that's not always the case for RSTA. Hopefully you can get more context here, e.g. does this occur before the component is realized or displayed? Thinking of "edge" cases here where things might not be fully initialized yet.

bobbylight commented 1 year ago

On second thought, do you have multiple threads doing initialization? I don't see how this exception is possible on the line indicated if all GUI work is done on the EDT:

pskowronek commented 1 year ago

In muC we kinda reworked how we initiate RSyntaxTextArea (https://github.com/mucommander/mucommander/issues/841) - since then we cannot produce the problem.

pskowronek commented 1 year ago

cannot reproduce in muC, closing