如何修复应用程序构造函数中的异常

我是 Javafx 的新手,我刚刚下载了 JDK 12 并遵循了教程,它有效但对我不起作用,(我正在使用模块来要求 javafx.controls)这里是代码:在我的主类中:


我在 SOFlow 中尝试了很多解决方案但没有结果,我尝试了:1)将 public 关键字添加到我的类中 2)删除了主要方法仍然不起作用帮助?


package com.teachersdunet.hellojavafx;


import javafx.application.Application;

import javafx.stage.Stage;


public class HelloApp extends Application {



      public static void main(String[] args) {

            Application.launch(args);

        }


    @Override

    public void start(Stage primaryStage) throws Exception {




    }



}

这是执行后的错误:


Exception in Application constructor

Exception in thread "main" java.lang.reflect.InvocationTargetException

    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

    at java.base/java.lang.reflect.Method.invoke(Method.java:567)

    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)

Caused by: java.lang.RuntimeException: Unable to construct Application instance: class com.teachersdunet.hellojavafx.HelloApp

    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:890)

    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)

    at java.base/java.lang.Thread.run(Thread.java:835)


Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class com.teachersdunet.hellojavafx.HelloApp (in module com.teachersdunet.hellojavafx) because module com.teachersdunet.hellojavafx does not export com.teachersdunet.hellojavafx to module javafx.graphics

    ... 1 more


哆啦的时光机
浏览 149回答 2
2回答

牛魔王的故事

解决方案几乎在堆栈跟踪中提到;问题缩小到它告诉您缺少的导出的程度:...Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class com.teachersdunet.hellojavafx.HelloApp (in module com.teachersdunet.hellojavafx) because module com.teachersdunet.hellojavafx does not export com.teachersdunet.hellojavafx to module javafx.graphics...将以下行添加到com.teachersdunet.hellojavafx模块中:module com.teachersdunet.hellojavafx {    ...    exports com.teachersdunet.hellojavafx;}或者只授予对单个模块的访问权限:module com.teachersdunet.hellojavafx {    ...    exports com.teachersdunet.hellojavafx to javafx.graphics;}

胡子哥哥

你会收到这样的错误,因为你没有在 start 方法中调用任何东西。您必须设置Scene并提供FXML文件的目录。我已经更正了你的代码。package com.teachersdunet.hellojavafx;import javafx.application.Application;import javafx.stage.Stage;public class HelloApp extends Application {    @Override    public void start(Stage primaryStage) throws Exception {       try {            Parent root = FXMLLoader.load(getClass().getResource("directory_of_your_fxml_file"));            Scene scene = new Scene(root);            primaryStage.setScene(scene);            primaryStage.show();       } catch(Exception e) {            e.printStackTrace();       }    }   public static void main(String[] args) {                launch(args);   }}我希望这有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java