【问题标题】:JavaFX fx:id NullPointerException [duplicate]JavaFX fx:id NullPointerException [重复]
【发布时间】:2020-12-18 11:02:28
【问题描述】:

为什么在这个简单的代码中,我得到一个异常? 我只是想在面板上加个按钮,搜索也找不到问题,感觉要么是我一个人,要么是一些基本的东西我不明白。 此代码执行唯一的功能,它只是在 fxml 内置的表单中添加一个按钮

public class Wtf extends Application {

@FXML
    public AnchorPane noteList;

    @Override
    public void start(Stage primaryStage) throws Exception {
        final FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("wtf.fxml"));
        final Parent root = loader.load();
        Button button = new Button();
        noteList.getChildren().add((button));


        Scene scene = new Scene(root);
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

FXML:

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

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


<Pane fx:controller="Wtf" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <AnchorPane fx:id="noteList" layoutX="119.0" layoutY="68.0" prefHeight="292.0" prefWidth="288.0">
         <children>
            <VBox layoutX="44.0" layoutY="14.0" prefHeight="207.0" prefWidth="190.0" />
         </children>
      </AnchorPane>
   </children>
</Pane>

堆栈跟踪:

Exception in Application start method
Exception in thread "main" 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 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$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at tests.Wtf.start(Wtf.java:25)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$3(WinApplication.java:177)
    ... 1 more

【问题讨论】:

  • 不要使用Application类作为控制器类。
  • 请告诉我如何解决这个问题?
  • 对控制器使用不同的类。您可以在其中定义一个initialize() 方法,该方法将在解析fxml 后自动调用。
  • 对不起,我不明白你的意思。最近开始学习javafx,还不是很了解。我只需要通过单击按钮将代码添加到现成的 fxml 表单中。我有一个应用程序,我必须通过单击按钮将新的 添加到 的位置。我可以将 fxml 错误地加载到控制器中吗?如何确定初始化?如果它不打扰您,请告诉我在哪里阅读。我阅读了 oracle 的说明,不幸的是我没有在那里找到我的问题的答案。
  • 我不知道如何使它更简单。我假设你知道什么是类,什么是方法。创建一个新类并将其用于控制​​器,并定义一个名为 initialize() 的方法,在加载 FXML 后,您可以在其中执行任何您需要执行的操作,访问任何具有 fx:id 属性的元素,就像您尝试做的那样在这里。

标签: java exception javafx nullpointerexception


【解决方案1】:

这对我有用,使用 FXML 文件放置按钮

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();

}


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

还有 FXML 文件

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

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


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" 
minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" 
xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
  <Button layoutX="274.0" layoutY="175.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>

编辑

如果您想使用 FX Id,请创建一个单独的类,因为它是一种更好的做法。像这样……

public class Main extends Application {


@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("ButtonTestClass.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();
}


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

用于按钮的新类

public class ButtonTestClass {

@FXML
private Button buttonId;

}

还有 FXML 文件...

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

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


<AnchorPane prefHeight="400.0" prefWidth="600.0" 
 xmlns="http://javafx.com/javafx/11.0.1" 
xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="sample.ButtonTestClass">
<children>
    <Button fx:id="buttonId" layoutX="331.0" layoutY="131.0" 
 mnemonicParsing="false" text="Button" />
    </children>
 </AnchorPane>

【讨论】:

  • 但是我无法通过代码中的 fx: id 使用此按钮,例如,我无法在其 fx: id 上调用 onAction 方法,我得到完全相同的错误
  • 刚刚根据您的需要进行了编辑。
  • 我已经有一个fxml格式的申请表了。它在控制器中运行。它有一个 AnchorPane,您需要向其中添加一个按钮,方法是单击哪些元素将添加到其 -> 中的同一 AnchorPane。这发生在在控制器中启动的阶段。我不明白单独的课程对我有什么帮助。我可以链接到一个成熟的应用程序并指出这个错误的位置,以便清楚我为什么需要 fx: id
  • gist.github.com/setaniel/1b652e8ab6023373dbccef299c862575App.java文件第58行第一次报错
  • 请注意,这不是个人帮助台!如果你不理解答案/cmets,你必须回去学习基础知识,直到你明白为止,没有办法;)
猜你喜欢
  • 2022-01-05
  • 1970-01-01
  • 2016-08-29
  • 2015-09-25
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多