How to fix Exception in Application constructor

拜拜、爱过 提交于 2021-02-10 14:38:43

问题


I'm new in Javafx, I just downloaded JDK 12 and followed a tuturial, it has worked but not worked for me, (I'm using module to require javafx.controls) here is the code: in my main class :

I tried a lot of solutions in SOFlow but no result, I tried : 1) add public keyword to my class 2) removed the main method still not work help ?

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 {



    }


}

and here is the error after executing it :

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
    at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:376)
    at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:639)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:490)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:802)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    ... 1 more

回答1:


The solution is almost mentioned in the stacktrace; the issue is narrowed down to the point where it tells you about the export that is missing:

...
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
...

Add the following line to the com.teachersdunet.hellojavafx module:

module com.teachersdunet.hellojavafx {
    ...
    exports com.teachersdunet.hellojavafx;
}

Or alternatively granting access to only a single module:

module com.teachersdunet.hellojavafx {
    ...
    exports com.teachersdunet.hellojavafx to javafx.graphics;
}



回答2:


You are getting an error like that because you haven't invoked anything inside the start method. You will have to set the Scene and also provide the directory of your FXML file.

I have corrected your code.

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);
   }
}

I hope this helps.



来源:https://stackoverflow.com/questions/57166325/how-to-fix-exception-in-application-constructor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!