Java FX FXML NullPointerException

时间:2017-12-24 08:02:16

标签: java javafx fxml

我没有使用Scene Builder和fxml的经验,今天当我开始处理我的新项目时遇到了设置我的fxml文件位置的问题。我也使用NetBeans 8.2。

这是我的应用程序类:

package javafxapplication;

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

/**
 *
 * @author Ben
 */
public class JavaFXApplication extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocumentLoad.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);
    }

}

我的FXML:

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9.0.1" fx:controller="javafxapplication.FXMLDocumentControllerTest">
    <children>
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
    </children>
</AnchorPane>

最后,我的控制器:

package javafxapplication;

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

/**
 *
 * @author Ben
 */
public class FXMLDocumentControllerTest implements Initializable {

    @FXML
    private Label label;

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

}

错误:

ant -f C:\\Users\\Ben\\Do7cuments\\NetBeansProjects\\JavaFXApplication -Djavac.includes=javafxapplication/JavaFXApplication.java -Dnb.internal.action.name=run.single -Drun.class=javafxapplication.JavaFXApplication run-single
init:
Deleting: C:\Users\Ben\Do7cuments\NetBeansProjects\JavaFXApplication\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\Ben\Do7cuments\NetBeansProjects\JavaFXApplication\build\built-jar.properties
Compiling 1 source file to C:\Users\Ben\Do7cuments\NetBeansProjects\JavaFXApplication\build\classes
compile-single:
run-single:
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: 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 javafxapplication.JavaFXApplication.start(JavaFXApplication.java:22)
    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)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application javafxapplication.JavaFXApplication
C:\Users\Ben\Do7cuments\NetBeansProjects\JavaFXApplication\nbproject\build-impl.xml:1052: The following error occurred while executing this line:
C:\Users\Ben\Do7cuments\NetBeansProjects\JavaFXApplication\nbproject\build-impl.xml:806: Java returned: 1
BUILD FAILED (total time: 0 seconds)

我的所有课程都在同一个文件夹&#34; / src / javafxapplication&#34; 我尝试将getResource更改为&#34; /FXMLDocumentLoad.fxml"和&#34; /javafxapplication/FXMLDocumentLoad.fxml",但都没有用

谢谢!

2 个答案:

答案 0 :(得分:0)

你不应该从getClass()得到一个空值.getResource(“file.fxml”);这是因为使用Class.getResource查找相对于类的资源。您说您已将所有资源放在同一目录和包中,因此使用该包中的类应该有效。

我注意到你使用了

FXMLLoader.load(getClass().getResource("FXMLDocumentLoad.fxml"));

这意味着 - 使用类加载器加载文件但是,类加载器不知道从中检索它的类;所以 - 它试图在默认包中找到FXMLDocumentLoad.fxml。

如果您使用类加载器,则应使用完整路径:

getClass().getClassLoader().getResource("/...path.../file.fxml")

答案 1 :(得分:0)

结果是NetBeans 8.2错误。解决方案是使用运行按钮运行文件,而不是执行shift-F6(pc)。