更改 CSS 时出现 NullPointerException 的无限流
Posted
技术标签:
【中文标题】更改 CSS 时出现 NullPointerException 的无限流【英文标题】:Infinite stream of NullPointerException when changing CSS 【发布时间】:2018-09-14 14:52:53 【问题描述】:我有一个包含多个控制器和阶段的应用程序。我试图通过 CSS 在我的所有其他控制器继承的父控制器类中实现可切换主题系统。它工作正常,除非我打开我的首选项对话框并关闭它。如果我在那之后尝试更改主题,我会在控制台中获得无限的 NullPointerExceptions 流,并且我的应用程序停止响应。同样重要的是要注意,如果我通过 IntelliJ 运行它,这不会发生,只有当我运行我的 jar 时才会发生。
大量相关代码和转储:
public class ControllerBase
protected static final String LOGGING_CONFIG_FILE_NAME = "/config/logging/log4j.xml";
public static SimpleStringProperty themeFilename = new SimpleStringProperty( );
protected Stage stage;
protected String cfgFile = new WorkbenchAppConfig().getFileName();
protected AppPropertiesManager prefs = new AppPropertiesManager();
protected Logger log;
protected ResourceBundle res;
protected ChangeListener CSSListener;
protected ControllerBase()
doInitsFromConfigFile();
String themeName = prefs.getString( cfgFile, KEY_GUI_THEME_ACTIVE, "Light" );
themeFilename.set( prefs.getString( cfgFile, KEY_GUI_THEMES_BASE + "." + themeName + ".filename" ));
CSSListener = new ChangeListener()
@Override
public void changed( ObservableValue observable, Object oldValue, Object newValue )
updateCss();
;
themeFilename.addListener( CSSListener );
protected void updateCss()
//Refresh Theme settings
if(stage != null)
ObservableList< String > css = stage.getScene().getStylesheets();
css.clear();
css.add( themeFilename.getValue() );
protected void doInitsFromConfigFile()
// get localized resources
String localeLanguage = prefs.getString(cfgFile, KEY_APP_LOCALE_LANGUAGE, "en");
String localeCountry = prefs.getString(cfgFile, KEY_APP_LOCALE_COUNTRY, "US");
res = CommonGuiUtils.getStringResourceBundle();
// get active GUI theme
String themeName = prefs.getString( cfgFile, KEY_GUI_THEME_ACTIVE, "Dark" );
themeFilename.set( prefs.getString( cfgFile, KEY_GUI_THEMES_BASE + "." + themeName + ".filename" ));
我的主控制器处理选择菜单的方法:
private void setUpThemeSelection()
lightingStyleGroup = new ToggleGroup();
lightingStyleGroup.getToggles().add( lightThemeOption );
lightingStyleGroup.getToggles().add( darkThemeOption );
String themeName = prefs.getString( cfgFile, KEY_GUI_THEME_ACTIVE, "Dark" );
ObservableList list = lightingStyleGroup.getToggles();
for ( Object x : list )
if ( ( (RadioMenuItem) x ).getText().startsWith( themeName ) )
lightingStyleGroup.selectToggle( (Toggle) x );
private void switchTheme( ActionEvent actionEvent )
//Find selected item and change prefs
RadioMenuItem selected = (RadioMenuItem) lightingStyleGroup.getSelectedToggle();
prefs.setString( cfgFile, KEY_GUI_THEME_ACTIVE, selected.getText() );
//Refresh Theme settings
String themeName = prefs.getString( cfgFile, KEY_GUI_THEME_ACTIVE, "Dark" );
themeFilename.set( prefs.getString( cfgFile, KEY_GUI_THEMES_BASE + "." + themeName + ".filename" ));
从控制台转储,删节
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.css.StyleManager.lambda$loadStylesheetUnPrivileged$191(StyleManager.java:1060)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:1054)
at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:935)
at com.sun.javafx.css.StyleManager.processStylesheets(StyleManager.java:1556)
at com.sun.javafx.css.StyleManager.gatherSceneStylesheets(StyleManager.java:1625)
at com.sun.javafx.css.StyleManager.findMatchingStyles(StyleManager.java:1663)
at javafx.scene.CssStyleHelper.createStyleHelper(CssStyleHelper.java:111)
at javafx.scene.Node.reapplyCss(Node.java:8985)
at javafx.scene.Node.impl_processCSS(Node.java:9182)
at javafx.scene.Parent.impl_processCSS(Parent.java:1249)
at javafx.scene.Node.processCSS(Node.java:9058)
at javafx.scene.Scene.doCSSPass(Scene.java:545)
at javafx.scene.Scene.access$3600(Scene.java:159)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2392)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:354)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:381)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:748)
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.css.StyleManager.lambda$loadStylesheetUnPrivileged$191(StyleManager.java:1060)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:1054)
at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:935)
at com.sun.javafx.css.StyleManager.processStylesheets(StyleManager.java:1556)
at com.sun.javafx.css.StyleManager.gatherSceneStylesheets(StyleManager.java:1625)
at com.sun.javafx.css.StyleManager.findMatchingStyles(StyleManager.java:1663)
at javafx.scene.CssStyleHelper.createStyleHelper(CssStyleHelper.java:111)
at javafx.scene.Node.reapplyCss(Node.java:8985)
at javafx.scene.Node.impl_processCSS(Node.java:9182)
at javafx.scene.Parent.impl_processCSS(Parent.java:1249)
at javafx.scene.Node.processCSS(Node.java:9058)
at javafx.scene.Scene.doCSSPass(Scene.java:545)
at javafx.scene.Scene.access$3600(Scene.java:159)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2392)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:354)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:381)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:748)
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.css.StyleManager.lambda$loadStylesheetUnPrivileged$191(StyleManager.java:1060)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:1054)
at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:935)
at com.sun.javafx.css.StyleManager.processStylesheets(StyleManager.java:1556)
at com.sun.javafx.css.StyleManager.gatherSceneStylesheets(StyleManager.java:1625)
at com.sun.javafx.css.StyleManager.findMatchingStyles(StyleManager.java:1663)
at javafx.scene.CssStyleHelper.createStyleHelper(CssStyleHelper.java:111)
at javafx.scene.Node.reapplyCss(Node.java:8985)
at javafx.scene.Node.impl_processCSS(Node.java:9182)
at javafx.scene.Parent.impl_processCSS(Parent.java:1249)
at javafx.scene.Node.processCSS(Node.java:9058)
at javafx.scene.Scene.doCSSPass(Scene.java:545)
at javafx.scene.Scene.access$3600(Scene.java:159)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2392)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:354)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:381)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:748)
...and so on forever
有人知道怎么回事吗?这是什么原因造成的?
【问题讨论】:
What is a NullPointerException, and how do I fix it?的可能重复 样式表值被更改为什么? jar 文件中的适当样式表是否位于预期位置? @James_D 据我所知正确的文件名,是的,它被打包在 jar 中 【参考方案1】:我发现了问题,我的首选项管理器有时会在错误的时间(看似随机)返回 null。我已经解决了这个问题。
【讨论】:
以上是关于更改 CSS 时出现 NullPointerException 的无限流的主要内容,如果未能解决你的问题,请参考以下文章
Hive 协议缓冲区 - 在 Hive 中创建表时出现 NullPointerException