【问题标题】:Launching fmxl GUI using javafx on Eclipse, Error in start method在 Eclipse 上使用 javafx 启动 fmxl GUI,启动方法出错
【发布时间】:2018-05-02 08:00:21
【问题描述】:

我正在尝试使用 eclipse 启动一个 fmxl 文件。

import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.application.Application;
import java.io.IOException;
import java.util.logging.Logger;
import java.util.logging.Level;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;

public class MainProgram extends Application {

public void start(Stage stage) {

    try {

        FXMLLoader fmxlLoader = new FXMLLoader();
        String viewerFxml = "Interface.fmxl";
        AnchorPane page = (AnchorPane)fmxlLoader.load(
                this.getClass().getResource(viewerFxml).openStream());
        Scene scene = new Scene(page);
        stage.setScene(scene);
        stage.show();

    } catch (IOException ex) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        System.exit(1);

    }
   }



public static void main(String[] args) {
    launch(args);
}

}

但是,我不断收到此错误。

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at     com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at MainProgram.start(MainProgram.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application MainProgram

代码似乎一直运行到这一行: AnchorPane 页面 = (AnchorPane)fmxlLoader.load( this.getClass().getResource(viewerFxml).openStream());

真的不知道为什么我会收到这个错误。

任何帮助将不胜感激。

【问题讨论】:

  • 如果 FXML 资源的路径不正确,您会收到该错误
  • (例如,你有Interface.fmxl,可能至少应该是Interface.fxml,尽管可能还有其他问题。)

标签: java eclipse javafx scenebuilder


【解决方案1】:

这是因为要加载的文件路径不正确。 尝试将正确的文件路径放入您的 String 变量中。 那是 String viewerFxml= "~path/Interface.fxml";

【讨论】:

  • 不,恐怕这行不通。 Interface.fxml 文件与 Eclipse 中的 MainProgram.class 文件位于同一个包中。我尝试了完整的路径,但我仍然遇到同样的错误。
  • 知道了。 .fmxl 而不是 .fxml 导致了这个问题。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
相关资源
最近更新 更多