JavaFX fx:id NullPointerException

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

JavaFX fx:id NullPointerException

问题

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

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

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(&quot;wtf.fxml&quot;));
        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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;?import java.lang.*?&gt;
&lt;?import javafx.scene.layout.*?&gt;


&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;
   &lt;children&gt;
      &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;
         &lt;children&gt;
            &lt;VBox layoutX=&quot;44.0&quot; layoutY=&quot;14.0&quot; prefHeight=&quot;207.0&quot; prefWidth=&quot;190.0&quot; /&gt;
         &lt;/children&gt;
      &lt;/AnchorPane&gt;
   &lt;/children&gt;
&lt;/Pane&gt;

StackTrace:

Exception in Application start method
Exception in thread &quot;main&quot; 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

答案1

得分: 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>
英文:

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

public class Main extends Application {

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

}


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

And the FXML file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;?import javafx.scene.control.Button?&gt;
&lt;?import javafx.scene.layout.AnchorPane?&gt;


&lt;AnchorPane 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/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot;&gt;
&lt;children&gt;
  &lt;Button layoutX=&quot;274.0&quot; layoutY=&quot;175.0&quot; mnemonicParsing=&quot;false&quot; text=&quot;Button&quot; /&gt;
&lt;/children&gt;
&lt;/AnchorPane&gt;

EDIT

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

public class Main extends Application {


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


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

New class used for the button

public class ButtonTestClass {

@FXML
private Button buttonId;

}

And the FXML File...

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;?import javafx.scene.control.Button?&gt;
&lt;?import javafx.scene.layout.AnchorPane?&gt;


&lt;AnchorPane prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; 
 xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; 
xmlns:fx=&quot;http://javafx.com/fxml/1&quot; 
fx:controller=&quot;sample.ButtonTestClass&quot;&gt;
&lt;children&gt;
    &lt;Button fx:id=&quot;buttonId&quot; layoutX=&quot;331.0&quot; layoutY=&quot;131.0&quot; 
 mnemonicParsing=&quot;false&quot; text=&quot;Button&quot; /&gt;
    &lt;/children&gt;
 &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:

确定