RSyntaxTextArea
RSyntaxTextArea copied to clipboard
NPE: RTextArea.setMarkAllHighlightColor when setting theme from updateUI
@Test
public void testSetTheme() {
RSyntaxTextArea rSyntaxTextArea = new RSyntaxTextArea() {
@Override
public void updateUI() {
super.updateUI();
try {
Theme.load(Theme.class.getResourceAsStream("themes/default.xml"))
.apply(this);
} catch (IOException e) {
throw new RuntimeException("theme load failed", e);
}
}
};
}
Output:
java.lang.NullPointerException
at org.fife.ui.rtextarea.RTextArea.setMarkAllHighlightColor(RTextArea.java:1507)
at org.fife.ui.rsyntaxtextarea.Theme.apply(Theme.java:194)
at ...Test$2.updateUI(...Test.java:191)
at java.desktop/javax.swing.text.JTextComponent.<init>(JTextComponent.java:326)
at java.desktop/javax.swing.JTextArea.<init>(JTextArea.java:199)
at java.desktop/javax.swing.JTextArea.<init>(JTextArea.java:135)
at org.fife.ui.rtextarea.RTextAreaBase.<init>(RTextAreaBase.java:81)
at org.fife.ui.rtextarea.RTextArea.<init>(RTextArea.java:191)
at org.fife.ui.rsyntaxtextarea.RSyntaxTextArea.<init>(RSyntaxTextArea.java:351)
at ...Test$2.<init>(...Test.java:185)
at ...Test.rsa(...Test.java:185)
This is admittedly a bug, but I consider it low priority since the fix requires adding a private field solely to store the state of the "mark all highlight color" for the sliver of time between the RSTA instance constructor starting and its init() method completing (which is during the constructor), and a simple workaround is to apply the theme in an overridden addNotify() method instead:
@Override
public void addNotify() {
super.addNotify();
try {
Theme.load(Theme.class.getResourceAsStream("themes/default.xml")).apply(this);
} catch (IOException e) {
throw new RuntimeException("theme load failed", e);
}
}
addNotify and updateUI are different APIs.
For instance, addNotify is not called when look and feel is changed, so if one wants to keep the control up to date, then the styling should happen somewhere near updateUI
There's a second case when this NPE might appear: SwingUI trying to install the theme from javax.swing.plaf.ComponentUI#installUI(JComponent) call.
Look and feel implementation might want to setup a common look for all the elements, so it might want to set certain parameters (e.g. RSTA theme) when the LaF is applied to the component.