英文:
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应用");
Label label = new Label("你好,世界,JavaFX!");
Scene scene = new Scene(label, 400, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String args[]) {
Application.launch(args);
}
}
这是堆栈跟踪。它显示了一个奇怪的NoSuchElementException异常。
在Application构造函数中的异常
在线程"main"中的异常java.lang.reflect.InvocationTargetException
在sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
在sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
在java.lang.reflect.Method.invoke(Unknown Source)
在sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
由于java.lang.RuntimeException: 无法构造Application实例:类xlSheets.javaFX
在com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
在com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
在java.lang.Thread.run(Unknown Source)
由于java.lang.NoSuchMethodException: xlSheets.javaFX.<init>()
在java.lang.Class.getConstructor0(Unknown Source)
在java.lang.Class.getConstructor(Unknown Source)
在com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$165(LauncherImpl.java:818)
在com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
在com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
在java.security.AccessController.doPrivileged(Native Method)
在com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
在com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
在com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
在com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
... 1 more
请注意,翻译文本中的HTML实体(如")已被还原为普通文本。
英文:
Other possible solutions I got in different forums are not working. Here is my code.
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);
}
}
This is the StackTrace. It is showing NoSuchElementException which is a bit weird.
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$launchApplication$159(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$launchApplication1$165(LauncherImpl.java:818)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(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$null$152(WinApplication.java:177)
... 1 more
答案1
得分: 2
Application.launch() 方法使用反射来创建 Application 子类的实例,调用其不带参数的构造函数。
根据文档,Application 子类
> 必须是 Application 的公共子类,具有公共的无参数构造函数。
因此,为了使其工作,无论是 Application 子类还是构造函数(如果显式定义)都需要声明为 public。
以下代码修复了这个问题(我还将类名更改为符合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);
}
}
英文:
The Application.launch() method uses reflection to create an instance of the Application subclass, calling its constructor taking no arguments.
According to the documentation the Appplication subclass
> must be a public subclass of Application with a public no-argument constructor
So in order for this to work, both the Application subclass and the constructor (if explicitly defined) need to be declared public.
The following fixes the problem (I also changed the class name to conform to Java naming conventions):
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);
}
}
专注分享java语言的经验与见解,让所有开发者获益!

评论