JavaFX fx:id NullPointerException

huangapple go评论175阅读模式
英文:

JavaFX fx:id NullPointerException

问题

为什么在这个简单的代码中,我得到了一个异常?
我只是尝试在面板中添加一个按钮,通过搜索我找不到问题,感觉要么只有我一个人碰到这个问题,要么我对一些基本概念理解有误。
这段代码执行唯一的功能,就是在通过FXML构建的表单中添加一个按钮。

  1. public class Wtf extends Application {
  2. @FXML
  3. public AnchorPane noteList;
  4. @Override
  5. public void start(Stage primaryStage) throws Exception {
  6. final FXMLLoader loader = new FXMLLoader();
  7. loader.setLocation(getClass().getResource("wtf.fxml"));
  8. final Parent root = loader.load();
  9. Button button = new Button();
  10. noteList.getChildren().add(button);
  11. Scene scene = new Scene(root);
  12. primaryStage.initStyle(StageStyle.TRANSPARENT);
  13. primaryStage.setScene(scene);
  14. primaryStage.show();
  15. }
  16. }

FXML:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import java.lang.*?>
  3. <?import javafx.scene.layout.*?>
  4. <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">
  5. <children>
  6. <AnchorPane fx:id="noteList" layoutX="119.0" layoutY="68.0" prefHeight="292.0" prefWidth="288.0">
  7. <children>
  8. <VBox layoutX="44.0" layoutY="14.0" prefHeight="207.0" prefWidth="190.0" />
  9. </children>
  10. </AnchorPane>
  11. </children>
  12. </Pane>

堆栈跟踪:

  1. Exception in Application start method
  2. Exception in thread "main" java.lang.reflect.InvocationTargetException
  3. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  4. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  5. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  6. at java.lang.reflect.Method.invoke(Method.java:498)
  7. at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
  8. Caused by: java.lang.RuntimeException: Exception in Application start method
  9. at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
  10. at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
  11. at java.lang.Thread.run(Thread.java:748)
  12. Caused by: java.lang.NullPointerException
  13. at tests.Wtf.start(Wtf.java:25)
  14. at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
  15. at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
  16. at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
  17. at java.security.AccessController.doPrivileged(Native Method)
  18. at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
  19. at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
  20. at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  21. at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
  22. ... 1 more
英文:

Why in this simple code, i'm get a exception?
I'm just trying to add a button to the panel, I can't find the problem by searching, the feeling that either I am the only one, or I do not understand some fundamental things.
This code performs the only function, it just adds a button to the form built in fxml

  1. public class Wtf extends Application {
  2. @FXML
  3. public AnchorPane noteList;
  4. @Override
  5. public void start(Stage primaryStage) throws Exception {
  6. final FXMLLoader loader = new FXMLLoader();
  7. loader.setLocation(getClass().getResource(&quot;wtf.fxml&quot;));
  8. final Parent root = loader.load();
  9. Button button = new Button();
  10. noteList.getChildren().add((button));
  11. Scene scene = new Scene(root);
  12. primaryStage.initStyle(StageStyle.TRANSPARENT);
  13. primaryStage.setScene(scene);
  14. primaryStage.show();
  15. }
  16. }

FXML:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;?import java.lang.*?&gt;
  3. &lt;?import javafx.scene.layout.*?&gt;
  4. &lt;Pane fx:controller=&quot;Wtf&quot; maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot; minWidth=&quot;-Infinity&quot; prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; xmlns=&quot;http://javafx.com/javafx/8&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot;&gt;
  5. &lt;children&gt;
  6. &lt;AnchorPane fx:id=&quot;noteList&quot; layoutX=&quot;119.0&quot; layoutY=&quot;68.0&quot; prefHeight=&quot;292.0&quot; prefWidth=&quot;288.0&quot;&gt;
  7. &lt;children&gt;
  8. &lt;VBox layoutX=&quot;44.0&quot; layoutY=&quot;14.0&quot; prefHeight=&quot;207.0&quot; prefWidth=&quot;190.0&quot; /&gt;
  9. &lt;/children&gt;
  10. &lt;/AnchorPane&gt;
  11. &lt;/children&gt;
  12. &lt;/Pane&gt;

StackTrace:

  1. Exception in Application start method
  2. Exception in thread &quot;main&quot; java.lang.reflect.InvocationTargetException
  3. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  4. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  5. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  6. at java.lang.reflect.Method.invoke(Method.java:498)
  7. at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
  8. Caused by: java.lang.RuntimeException: Exception in Application start method
  9. at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
  10. at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
  11. at java.lang.Thread.run(Thread.java:748)
  12. Caused by: java.lang.NullPointerException
  13. at tests.Wtf.start(Wtf.java:25)
  14. at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
  15. at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
  16. at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
  17. at java.security.AccessController.doPrivileged(Native Method)
  18. at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
  19. at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
  20. at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  21. at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
  22. ... 1 more

答案1

得分: 1

以下是您提供的内容的翻译:

这对我来说是有效的,使用FXML文件放置按钮

  1. public class Main extends Application {
  2. @Override
  3. public void start(Stage primaryStage) throws Exception{
  4. Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
  5. primaryStage.setTitle("Hello World");
  6. primaryStage.setScene(new Scene(root, 300, 275));
  7. primaryStage.show();
  8. }
  9. public static void main(String[] args) {
  10. launch(args);
  11. }
  12. }

FXML文件内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.Button?>
  3. <?import javafx.scene.layout.AnchorPane?>
  4. <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
  5. minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
  6. xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
  7. <children>
  8. <Button layoutX="274.0" layoutY="175.0" mnemonicParsing="false" text="Button" />
  9. </children>
  10. </AnchorPane>

编辑

如果您想要使用FX ID,那么创建一个单独的类会是一个更好的实践。像这样...

  1. public class Main extends Application {
  2. @Override
  3. public void start(Stage primaryStage) throws Exception{
  4. Parent root = FXMLLoader.load(getClass().getResource("ButtonTestClass.fxml"));
  5. primaryStage.setTitle("Hello World");
  6. primaryStage.setScene(new Scene(root, 300, 275));
  7. primaryStage.show();
  8. }
  9. public static void main(String[] args) {
  10. launch(args);
  11. }
  12. }

用于按钮的新类:

  1. public class ButtonTestClass {
  2. @FXML
  3. private Button buttonId;
  4. }

以及FXML文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.Button?>
  3. <?import javafx.scene.layout.AnchorPane?>
  4. <AnchorPane prefHeight="400.0" prefWidth="600.0"
  5. xmlns="http://javafx.com/javafx/11.0.1"
  6. xmlns:fx="http://javafx.com/fxml/1"
  7. fx:controller="sample.ButtonTestClass">
  8. <children>
  9. <Button fx:id="buttonId" layoutX="331.0" layoutY="131.0"
  10. mnemonicParsing="false" text="Button" />
  11. </children>
  12. </AnchorPane>
英文:

This works for me, using the FXML file to place the button

  1. public class Main extends Application {
  2. @Override
  3. public void start(Stage primaryStage) throws Exception{
  4. Parent root = FXMLLoader.load(getClass().getResource(&quot;sample.fxml&quot;));
  5. primaryStage.setTitle(&quot;Hello World&quot;);
  6. primaryStage.setScene(new Scene(root, 300, 275));
  7. primaryStage.show();
  8. }
  9. public static void main(String[] args) {
  10. launch(args);
  11. }
  12. }

And the FXML file

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;?import javafx.scene.control.Button?&gt;
  3. &lt;?import javafx.scene.layout.AnchorPane?&gt;
  4. &lt;AnchorPane maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot;
  5. minWidth=&quot;-Infinity&quot; prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot;
  6. xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot;&gt;
  7. &lt;children&gt;
  8. &lt;Button layoutX=&quot;274.0&quot; layoutY=&quot;175.0&quot; mnemonicParsing=&quot;false&quot; text=&quot;Button&quot; /&gt;
  9. &lt;/children&gt;
  10. &lt;/AnchorPane&gt;

EDIT

If you want to use the FX Id then make a seperate Class since its a better practice. Like this...

  1. public class Main extends Application {
  2. @Override
  3. public void start(Stage primaryStage) throws Exception{
  4. Parent root = FXMLLoader.load(getClass().getResource(&quot;ButtonTestClass.fxml&quot;));
  5. primaryStage.setTitle(&quot;Hello World&quot;);
  6. primaryStage.setScene(new Scene(root, 300, 275));
  7. primaryStage.show();
  8. }
  9. public static void main(String[] args) {
  10. launch(args);
  11. }
  12. }

New class used for the button

  1. public class ButtonTestClass {
  2. @FXML
  3. private Button buttonId;
  4. }

And the FXML File...

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;?import javafx.scene.control.Button?&gt;
  3. &lt;?import javafx.scene.layout.AnchorPane?&gt;
  4. &lt;AnchorPane prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot;
  5. xmlns=&quot;http://javafx.com/javafx/11.0.1&quot;
  6. xmlns:fx=&quot;http://javafx.com/fxml/1&quot;
  7. fx:controller=&quot;sample.ButtonTestClass&quot;&gt;
  8. &lt;children&gt;
  9. &lt;Button fx:id=&quot;buttonId&quot; layoutX=&quot;331.0&quot; layoutY=&quot;131.0&quot;
  10. mnemonicParsing=&quot;false&quot; text=&quot;Button&quot; /&gt;
  11. &lt;/children&gt;
  12. &lt;/AnchorPane&gt;

huangapple
  • 本文由 发表于 2020年8月30日 05:28:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63651949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定