Exception in Application constructor when using Ja

2020-05-06 22:58发布

问题:

I recently started learning JavaFX, and got this code from a book that is using JDK 9. When I ran the program I got errors. I looked at other code and tried adding '@Override' and "throws Exception" to the start method, however I got the same errors. I get no errors when building the program.

If it helps: I am using JDK 11.0.2, and JavaFX from openjfx.io.

import javafx.application.*;
import javafx.scene.*;
importjavafx.stage.*;
import javafx.scene.layout.*;

class JavaFXSkel extends Application{

public static void main(String[] args){
    System.out.println("Launching JavaFX Application");

    launch(args);
}

public void init(){
    System.out.println("Inside the init() method");
}


public void start(Stage myStage){
    System.out.println("Inside the start() method");

    myStage.setTitle("JavaFX Skeleton");

    //makes a root node with a flow layout pane
    FlowPane rootNode = new FlowPane();

    //Crate a scne
    Scene myScene = new Scene(rootNode, 300, 200);

    //Set teh scene on stage
    myStage.setScene(myScene);

    //Show the stage and the scene
    myStage.show();
}

public void stop(){
    System.out.println("Inside the stop() method");
}
}

Error Messages:

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:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class JavaFXSkel
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:834)
Caused by: java.lang.NoSuchMethodException: JavaFXSkel.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3350)
at java.base/java.lang.Class.getConstructor(Class.java:2152)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:801)
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(Native Method)
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)

回答1:

Your JavaFXSkel class must be public, as stated in the documentation of Application:

The Application subclass must be declared public and must have a public no-argument constructor.

The JavaFX runtime instantiates an instance of your Application subclass using reflection. It does this by using the public no-arg constructor of the class. However, your JavaFXSkel class doesn't explicitly declare any constructors which means it has the implicit default constructor. The default constructor has the same access modifier as the enclosing class, which is the default package access in your case. This is mentioned in §8.8.9 of the Java Language Specification1:

If a class contains no constructor declarations, then a default constructor is implicitly declared. The form of the default constructor for a top level class, member class, or local class is as follows:

  • The default constructor has the same access modifier as the class, unless the class lacks an access modifier, in which case the default constructor has package access (§6.6).

  • The default constructor has no formal parameters, except in a non-private inner member class, where the default constructor implicitly declares one formal parameter representing the immediately enclosing instance of the class (§8.8.1, §15.9.2, §15.9.3).

What this means is your JavaFXSkel has no public no-arg constructor, thus the NoSuchMethodException.


1. Don't expect a beginner to have read much, if any, of the JLS; I'm just providing it as an official reference.



回答2:

Works for me using JDK 11.0.2 tools on Windows 10. Made one change to your source code:

public class JavaFXSkel

Saved your code in a file named "JavaFXSkel.java"

Compiled the code with the following command.

javac --module-path "C:\Program Files\Java\javafx-sdk-11.0.2\lib" --add-modules=javafx.controls -g JavaFXSkel.java

Ran the program with the following command:

java --module-path "C:\Program Files\Java\javafx-sdk-11.0.2\lib" --add-modules=javafx.controls JavaFXSkel

This Web page helped: https://openjfx.io/openjfx-docs/

Here is a screen capture of the app running.



标签: java javafx