Exception in thread “JavaFX Application Thread” java.util.ConcurrentModificationException

huangapple go评论79阅读模式
英文:

Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException

问题

我在尝试在JavaFx中显示舞台时遇到了这个错误,已经尝试了多个可用的答案,但都没有起作用。

我的代码:

private void redirectToLogin() {
    try {
        Parent root = null;
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/login.fxml"));
        fxmlLoader.setResources(resourceBundle);
        root = fxmlLoader.load();
        Stage primaryStage = new Stage();
        new Thread(() -> {
            logger.info("Initializing tray icon");
            App.TRAY_ICON = new TrayIcon();
            App.TRAY_ICON.displayingTrayIcon(userService, downloadingService, schedulerService, primaryStage);
        }).start();
        primaryStage.setTitle(resourceBundle.getString("appTitle"));
        primaryStage.getIcons().add(new javafx.scene.image.Image(this.getClass().getResource("/images/test.png").toString()));
        Scene scene = new Scene(root, 800, 600);
        scene.getStylesheets().add(getClass().getResource("/css/test.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        System.out.println("Calling show");
        primaryStage.show();
        System.out.println("show called");
        Platform.setImplicitExit(false);
    } catch (Exception e) {
        System.out.println("Exception in appinstall : "+e);
        e.printStackTrace();
    }
}

完整的错误堆栈:

Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
at com.sun.javafx.font.PrismFontFactory.removeEmbeddedFont(PrismFontFactory.java:1554)
at com.sun.javafx.font.PrismFontFactory.loadEmbeddedFont(PrismFontFactory.java:1638)
at com.sun.javafx.font.PrismFontFactory.loadEmbeddedFont(PrismFontFactory.java:1529)
at com.sun.javafx.font.PrismFontLoader.loadFont(PrismFontLoader.java:99)
at javafx.scene.text.Font.loadFont(Font.java:400)
at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:1001)
at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:798)
at com.sun.javafx.css.StyleManager.gatherParentStylesheets(StyleManager.java:1285)
at com.sun.javafx.css.StyleManager.findMatchingStyles(StyleManager.java:1400)
at javafx.scene.CssStyleHelper.createStyleHelper(CssStyleHelper.java:115)
at javafx.scene.Node.impl_processCSS(Node.java:8877)
at javafx.scene.Parent.impl_processCSS(Parent.java:1250)
at javafx.scene.control.Control.impl_processCSS(Control.java:872)
at javafx.scene.Parent.impl_processCSS(Parent.java:1281)
at javafx.scene.Parent.impl_processCSS(Parent.java:1281)
at javafx.scene.Parent.impl_processCSS(Parent.java:1281)
at javafx.scene.Parent.impl_processCSS(Parent.java:1281)
at javafx.scene.Parent.impl_processCSS(Parent.java:1281)
at javafx.scene.Parent.impl_processCSS(Parent.java:1281)
at javafx.scene.Parent.impl_processCSS(Parent.java:1281)
at javafx.scene.Node.processCSS(Node.java:8732)
at javafx.scene.Scene.doCSSPass(Scene.java:569)
at javafx.scene.Scene.access$3500(Scene.java:201)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2385)
at com.sun.javafx.tk.Toolkit$3.run(Toolkit.java:321)
at com.sun.javafx.tk.Toolkit$3.run(Toolkit.java:319)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:319)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:348)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:479)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:460)
at com.sun.javafx.tk.quantum.QuantumToolkit$13.run(QuantumToolkit.java:327)
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.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:745)

所有路径/URI已经经过了双重检查,即图标、CSS和FXML都存在于各自的位置。

请协助解决。

英文:

I am getting this error while trying to show stage in JavaFx, tried multiple available answers already but none of them worked.

My code

private void redirectToLogin() {
        try {
            Parent root = null;
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/login.fxml"));
            fxmlLoader.setResources(resourceBundle);
            root = fxmlLoader.load();
            Stage primaryStage = new Stage();
            new Thread(() -> {
                logger.info("Initializing tray icon");
                App.TRAY_ICON = new TrayIcon();
                App.TRAY_ICON.displayingTrayIcon(userService, downloadingService, schedulerService, primaryStage);
            }).start();
            primaryStage.setTitle(resourceBundle.getString("appTitle"));
            primaryStage.getIcons().add(new javafx.scene.image.Image(this.getClass().getResource("/images/test.png").toString()));
            Scene scene = new Scene(root, 800, 600);
        scene.getStylesheets().add(getClass().getResource("/css/test.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.setResizable(false);
            System.out.println("Calling show");
            primaryStage.show();
            System.out.println("show called");
            Platform.setImplicitExit(false);
        } catch (Exception e) {
            System.out.println("Exception in appinstall : "+e);
            e.printStackTrace();
        }
    }

Full Error stack:

Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
at com.sun.javafx.font.PrismFontFactory.removeEmbeddedFont(PrismFontFactory.java:1554)
at com.sun.javafx.font.PrismFontFactory.loadEmbeddedFont(PrismFontFactory.java:1638)
at com.sun.javafx.font.PrismFontFactory.loadEmbeddedFont(PrismFontFactory.java:1529)
at com.sun.javafx.font.PrismFontLoader.loadFont(PrismFontLoader.java:99)
at javafx.scene.text.Font.loadFont(Font.java:400)
at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:1001)
at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:798)
at com.sun.javafx.css.StyleManager.gatherParentStylesheets(StyleManager.java:1285)
at com.sun.javafx.css.StyleManager.findMatchingStyles(StyleManager.java:1400)
at javafx.scene.CssStyleHelper.createStyleHelper(CssStyleHelper.java:115)
at javafx.scene.Node.impl_processCSS(Node.java:8877)
at javafx.scene.Parent.impl_processCSS(Parent.java:1250)
at javafx.scene.control.Control.impl_processCSS(Control.java:872)
at javafx.scene.Parent.impl_processCSS(Parent.java:1281)
at javafx.scene.Parent.impl_processCSS(Parent.java:1281)
at javafx.scene.Parent.impl_processCSS(Parent.java:1281)
at javafx.scene.Parent.impl_processCSS(Parent.java:1281)
at javafx.scene.Parent.impl_processCSS(Parent.java:1281)
at javafx.scene.Parent.impl_processCSS(Parent.java:1281)
at javafx.scene.Node.processCSS(Node.java:8732)
at javafx.scene.Scene.doCSSPass(Scene.java:569)
at javafx.scene.Scene.access$3500(Scene.java:201)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2385)
at com.sun.javafx.tk.Toolkit$3.run(Toolkit.java:321)
at com.sun.javafx.tk.Toolkit$3.run(Toolkit.java:319)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:319)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:348)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:479)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:460)
at com.sun.javafx.tk.quantum.QuantumToolkit$13.run(QuantumToolkit.java:327)
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.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:745)

All paths/URIs are double-checked already i.e icon, CSS, and FXML exists at the there respective location.

Please help

答案1

得分: 0

你正在从不同的线程访问一个非并发哈希映射。如果您需要从不同的线程访问同一哈希映射变量,必须使用:

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html

也许是从displayingTrayIcon函数中进行的访问?

英文:

You are accessing to a non cocurrent hashmap from different threads. If you need to access the same hashmap variable from different threads you must use :

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html

Maybe from displayingTrayIcon function?

huangapple
  • 本文由 发表于 2020年10月14日 20:53:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64353721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定