【问题标题】:JavaFX, nullpointerexceltion, Location is requiredJavaFX,nullpointerexceltion,位置是必需的
【发布时间】:2015-04-18 01:07:45
【问题描述】:

我班上的很多人都遇到了这个错误,而我现在就是那个在苦苦挣扎的人。我用谷歌搜索了一下,这似乎是新程序员的一个普遍问题..

我正在尝试启动我的 JavaFX Gui 应用程序,但是

 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:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
    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:483)
    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:875)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/1598924227.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3201)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
    at application.Main.start(Main.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$56/662377048.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$50/1268447657.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$52/2028564720.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$51/1851691492.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
     at    com.sun.glass.ui.gtk.GtkApplication.lambda$null$45(GtkApplication.java:126  )
        at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/584634336.run(Unknown     Source)
    ... 1 more
  Exception running application application.Main

我可以看到问题。它找不到我的 FXML 文件,这就是问题所在,但我该如何解决呢?我做错了什么?

【问题讨论】:

标签: java user-interface error-handling javafx syntax-error


【解决方案1】:

从您的文件中删除 '/'。它不会找到 "/FXML.fxml"

【讨论】:

  • 否则尝试像这样拆分FXMLLoader loader = new FXMLLoader(this.getClass().getResource("DefaultFrame.fxml")); Parent root = (Parent) loader.load();
  • 谢谢,但仍然出现同样的错误。它无法加载 FXML。
  • 嗯,您的控制器是在 FXML 中设置的吗? FXML 文件中应该有属性fx:controller="defaultframe.DefaultFrameController"。如果不是,请将其添加到 TopFrame。
  • 什么是defaultFrame和defaultFrameController?我的控制器是FXMLController...我很困惑,对不起。
  • 抱歉在你的文件中使用了我自己的术语^^ 它应该是fx:controller="application.FXMLController"
【解决方案2】:

为什么在您的位置添加“/”?这不是必需的,因为您的 fxml 文件位于同一文件夹中,而不是在下面的文件夹中。所以删除“/”。

【讨论】:

  • 也许你的问题是,你的控制器没有设置。您想为您的 FXML 使用您的 FXML 控制器吗?所以你必须首先设置你的控制器: FXMLController controller = new FXMLController(): FXMLLoader FXMLLoader=new FXMLLoader(Main.getClass().getResource("FXML.fxml")); FXMLLoader.setController(控制器);父根 = FXMLLoader.load();
  • 如果没有控制器,您的 fxml UI 将无法执行任何操作;另一种可能性是在您的 fxml 文件中定义您的控制器,就像在 stackoverflow.com/questions/20507591/… 中所做的那样
  • 谢谢,但仍然出现同样的错误。它无法加载 FXML。
  • getResource("FXML.fxml") 里面有空格吗?这可能会导致问题
猜你喜欢
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 2019-03-26
  • 1970-01-01
  • 2013-05-17
  • 2014-02-03
相关资源
最近更新 更多