gpt4 book ai didi

JavaFX NullPointerException 位置是必需的 NetBeans

转载 作者:行者123 更新时间:2023-12-01 20:03:34 25 4
gpt4 key购买 nike

我已经尝试了在堆栈 stackoverflow 上找到的其他任何内容,但我真的不明白为什么这不起作用。我不会向您展示我的应用程序无法正常工作的代码,因为即使在示例项目中它也无法正常工作。那么问题来了:

当我使用示例代码创建新的 JavaFX 应用程序时,该按钮提供单击后打印 hello world 的按钮,当我将其作为桌面应用程序运行以及构建它并在浏览器中启动时,它会起作用。这可以完美地用作桌面和浏览器应用程序

但是当我创建新的 JavaFX FXML 应用程序时,它与上面几乎相同,但阶段是由 fxml 和 css 而不是字节代码定义的。这个应用程序可以完美地用作 Windows 应用程序,但不能用作浏览器应用程序

java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafxapplication3.JavaFXApplication3.start(JavaFXApplication3.java:22)
at com.sun.javafx.applet.FXApplet2$2.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/15592694.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/19532686.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$35/9825943.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.NullPointerException: Location is required.
at com.sun.javafx.applet.FXApplet2$2.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/15592694.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/19532686.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$35/9825943.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafxapplication3.JavaFXApplication3.start(JavaFXApplication3.java:22)
... 11 more

这是工作应用程序的代码:

public class JavaFXApplication4 extends Application {

@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});

StackPane root = new StackPane();
root.getChildren().add(btn);

Scene scene = new Scene(root, 300, 250);

primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

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

此应用程序无法在浏览器中运行并引发异常:

public class JavaFXApplication3 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);
}

}

有人可以帮助我吗?

最佳答案

问题出在这一行:

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

很明显,load 方法实际上是用 null 参数调用的。发生这种情况是因为 getResource("/sample/sample.fxml") 无法归档该资源。

运行时类路径上缺少资源(或路径错误)。

Source

关于JavaFX NullPointerException 位置是必需的 NetBeans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31279442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com