【问题标题】:JavaFX Location is required NullPointerException NetbeansJavaFX 位置是必需的 NullPointerException Netbeans
【发布时间】:2015-12-01 19:29:41
【问题描述】:

因此,我尝试构建 JavaFX 应用程序,但收到 NullPointerException“需要位置”。我已经找到了几个与此相关的问题,但没有一个对我有用。

我已经试过了:

Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("FXMLDocument.fxml"));

Parent root = FXMLLoader.load(getClass().getResource("/FXMLDocument.fxml"));

并将 FXMLDocument.fxml 移动到默认包,但这些都不适合我。 这是 Netbeans 创建的原始 JavaFX FXML 应用程序,我真的不明白为什么它不能开箱即用......

提前致谢!

项目/文件结构:

Test.java:

package test;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Tim
 */
public class Test extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

FXMLDocument.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.FXMLDocumentController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
    </children>
</AnchorPane>

FXMLDocumentController.java:

package test;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

/**
 *
 * @author Tim
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

堆栈跟踪:

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:497)
    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:497)
    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$156(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at test.Test.start(Test.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$149(WinApplication.java:191)
    ... 1 more
Exception running application test.Test

【问题讨论】:

  • 你应该看看 getResource 返回什么,这可能不是什么明智的,因为你的 fxml 不是你的 .class 文件所在的位置,看起来
  • 您是否尝试在另一个项目中从头开始创建您的工作区?因为我复制/粘贴了你的所有文件,它似乎适用于 Netbean 8.0.2。
  • 由于某种原因,控制器类没有编译,FXML文件也没有部署。尝试清理并重建项目。
  • 我确认了@James_D 在我的项目中所说的话,我可以在 test/build/classes 中看到 fxml 和类文件。
  • 感谢到目前为止的帮助,我尝试调试,看起来 getResource() 返回 null.. 但我不知道为什么.. 我从头开始创建项目,它是 netbeans FXML 应用程序模板..我在我的电脑上用 Netbeans 8.0.2、8.1 和我的笔记本电脑用 8.0.2 测试过 - 错误保持不变

标签: javafx nullpointerexception fxml


【解决方案1】:

@James_D 是对的,需要清理和重建项目(Shift+F11)(尽管我不知道为什么,因为我是通过 Netbeans FXML 应用程序模板从头开始创建项目的......)

【讨论】:

    【解决方案2】:

    如果该方法根本不起作用,您必须清理并构建您的项目。这对我有用

    【讨论】:

      【解决方案3】:

      我有同样的问题,可以解决。我的答案在这里JavaFX NullPointerException Location is required NetBeans。在 Netbeans 8.2 的 Build > Deployment 下的项目属性中,有一个复选框“请求不受限制的访问(启用签名)”。

      【讨论】:

        猜你喜欢
        • 2015-09-25
        • 2015-04-18
        • 1970-01-01
        • 1970-01-01
        • 2014-03-26
        • 2019-03-26
        • 1970-01-01
        • 2019-11-17
        • 2013-05-17
        相关资源
        最近更新 更多