JavaFX中的NPE在控制器中调用字段时

时间:2014-09-30 22:58:32

标签: nullpointerexception javafx

我想让我的未修饰的场景变得可拖动。我的根布局包含带有控件和内容的工具栏。我为工具栏提供了id并为.fxml布局设置了控制器。 然后我执行Dragging an undecorated Stage in JavaFX中的所有步骤 但是当我尝试调用EffectUtilities.makeDraggable(stage,tool_bar)时,我有空指针; 这是代码:

主要

    public class UpdaterMain extends Application{

    private Stage primaryStage;
    private BorderPane rootLayout;

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;
        this.primaryStage.initStyle(StageStyle.UNDECORATED);
        this.primaryStage.initStyle(StageStyle.TRANSPARENT);

        initRootLayout();
        showContentOverview();

    }

    /**
     * Initializes the root layout.
     */
    public void initRootLayout() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(UpdaterMain.class.getResource("RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();

            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);

            UpdaterMainController controller =
                    loader.getController();
            controller.registerStage(primaryStage); <<--Here is NPE

            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Shows main content
     */
    public void showContentOverview() {
        try {
            // Load person overview.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(UpdaterMain.class.getResource("updater-layout.fxml"));
            SplitPane contentOverview = loader.load();

            // Set person overview into the center of root layout.
            rootLayout.setCenter(contentOverview);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

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

控制器

import com.iminegination.utils.EffectUtilities;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.stage.Stage;

public class UpdaterMainController {
    @FXML
    private ToolBar tool_bar;

    public void registerStage(Stage stage) {
        EffectUtilities.makeDraggable(stage, tool_bar); <<--Here is NPE (tool_bar is null, but why?)
    }

    public UpdaterMainController() {
    }
}

layout.fxml

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="690.0" prefWidth="1024.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.iminegination.updater.view.UpdaterMainController">
   <top>
      <ToolBar id="tool_bar" nodeOrientation="RIGHT_TO_LEFT" prefHeight="40.0" prefWidth="200.0">
        <items>
          <Button id="exitButton" mnemonicParsing="false" text="X" onAction="#click"/>
            <Button mnemonicParsing="false" text="Settings" />
            <Button mnemonicParsing="false" text="_" />
        </items>
      </ToolBar>
   </top>
</BorderPane>

1 个答案:

答案 0 :(得分:1)

您需要在FXML中为控件定义fx:id属性。

在你的FXML中你有:

<ToolBar id="tool_bar" . . .

你应该:

<ToolBar id="tool_bar" fx:id="tool_bar" . . .

id定义了CSS ID。

fx:id定义了FXML和控制器变量名之间的链接。