Stack trace 中出现 NoSuchElementException,尝试了社区中的一些其他解决方案仍然没有帮助

NoSuchElementException in Stack trace, tried some other solutions in the community still not helpful

我在不同论坛上获得的其他可能的解决方案均无效。这是我的代码。

class javaFX extends Application{

    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.setTitle("JavaFX app");
        Label label = new Label("Hello World, JavaFX !");
        Scene scene = new Scene(label, 400, 200);
        primaryStage.setScene(scene);
    
        primaryStage.show();
    }
    public static void main(String args[]) {
        Application.launch(args);
    }
}

这是 StackTrace。它显示的 NoSuchElementException 有点奇怪。

Exception in Application constructor
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class xlSheets.javaFX
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication9(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: xlSheets.javaFX.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication15(LauncherImpl.java:818)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait9(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null7(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater8(PlatformImpl.java:294)
    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$null2(WinApplication.java:177)
    ... 1 more

Application.launch() 方法使用反射创建 Application subclass 的实例,调用其构造函数不带参数。

根据documentation Appplication子class

must be a public subclass of Application with a public no-argument constructor

因此,为了使其工作,Application subclass 和构造函数(如果明确定义)都需要声明 public.

以下修复了问题(我还更改了 class 名称以符合 Java 命名约定):

public class JavaFX extends Application{

    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.setTitle("JavaFX app");
        Label label = new Label("Hello World, JavaFX !");
        Scene scene = new Scene(label, 400, 200);
        primaryStage.setScene(scene);
    
        primaryStage.show();
    }
    public static void main(String args[]) {
        Application.launch(args);
    }
}