0

我在 Scene Builder 中生成了 FXML:

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

<?scenebuilder-preview-i18n-resource ../lang/ru_RU.properties?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" fx:id="mainStagePane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="UI.MainStageController">
  <children>
    <SplitPane dividerPositions="0.6596244131455399" focusTraversable="true" prefHeight="-1.0" prefWidth="1280.0" AnchorPane.bottomAnchor="30.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="160.0">
      <items>
        <AnchorPane id="rootPane" fx:id="pane3d" prefHeight="930.0" prefWidth="1280.0" />
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
          <children>
            <Accordion id="accordeon" fx:id="accordion" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
              <panes>
                <TitledPane animated="true" text="%3dSceneSettings" fx:id="x2">
                  <content>
                    <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                      <children>
                        <Slider fx:id="farClipSlider" layoutY="50.0" majorTickUnit="10.0" max="100.0" min="-100.0" minorTickCount="1" prefWidth="337.0" showTickLabels="true" showTickMarks="true" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="75.0" />
                        <TextField layoutY="14.0" prefWidth="55.0" AnchorPane.rightAnchor="217.0" />
                        <Slider fx:id="distanceSlider" blockIncrement="25.0" layoutX="14.0" layoutY="133.0" majorTickUnit="50.0" max="500.0" min="-500.0" minorTickCount="25" prefWidth="399.0" showTickLabels="true" showTickMarks="true" snapToTicks="true" />
                        <VBox layoutY="364.0" prefHeight="200.0" prefWidth="100.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0">
                          <children>
                            <HBox prefHeight="-1.0" prefWidth="-1.0">
                              <children>
                                <Label text="Label" />
                                <TextField prefWidth="55.0" />
                              </children>
                            </HBox>
                            <Slider fx:id="nearClipSlider" blockIncrement="1.0" majorTickUnit="10.0" max="100.0" min="-100.0" minorTickCount="1" prefWidth="338.0" showTickLabels="true" showTickMarks="true" snapToTicks="true" value="0.0" />
                          </children>
                        </VBox>
                        <Label layoutX="104.0" layoutY="17.0" text="Label" />
                      </children>
                    </AnchorPane>
                  </content>
                </TitledPane>
                <TitledPane animated="true" expanded="false" text="%3dModelSettings" fx:id="x1">
                  <content>
                    <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
                  </content>
                </TitledPane>
              </panes>
            </Accordion>
          </children>
        </AnchorPane>
      </items>
    </SplitPane>
    <ToolBar maxHeight="30.0" minHeight="30.0" prefHeight="30.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
      <items>
        <ProgressBar fx:id="loadProgressBar" disable="false" prefWidth="200.0" progress="0.0" visible="true" />
      </items>
    </ToolBar>
    <TabPane prefHeight="160.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <tabs>
        <Tab text="%file">
          <content>
            <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
              <children>
                <Button alignment="CENTER" graphicTextGap="4.0" mnemonicParsing="false" prefHeight="90.0" prefWidth="90.0" text="%openFile" textAlignment="CENTER" textOverrun="ELLIPSIS" underline="false" wrapText="true" AnchorPane.leftAnchor="14.0" AnchorPane.topAnchor="14.0" />
                <Separator layoutX="170.0" orientation="VERTICAL" prefHeight="200.0" AnchorPane.bottomAnchor="10.0" AnchorPane.topAnchor="10.0" />
              </children>
            </AnchorPane>
          </content>
        </Tab>
        <Tab text="%analysis">
          <content>
            <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
              <children>
                <Button mnemonicParsing="false" prefHeight="90.0" prefWidth="90.0" text="Button" AnchorPane.leftAnchor="14.0" AnchorPane.topAnchor="14.0" />
              </children>
            </AnchorPane>
          </content>
        </Tab>
      </tabs>
    </TabPane>
  </children>
</AnchorPane>

但突然我收到以下异常:

javafx.fxml.LoadException: 
/D:/Projects/CastAnalytics/out/production/CastAnalytics/UI/MainStage.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2592)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2570)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2416)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3160)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3121)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3094)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3070)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3062)
    at core.CastAnalytics.start(CastAnalytics.java:32)
    at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
    at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:331)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:297)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(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.access$300(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
    at UI.MainStageController.set(MainStageController.java:98)
    at UI.MainStageController.initialize(MainStageController.java:34)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2523)
    ... 18 more

这是什么意思,我该如何解决?

4

2 回答 2

0

好吧,您在 MainStageController 的第 98 行有一个空指针......

如果我不得不猜测,我会说您在 Controller 中定义的 @FXML 变量之一的名称与 FXML 文件中的名称不同,因此您得到的是 NPE。

您必须粘贴相关的 MainStageController.java 代码,以便任何人了解更多信息。

于 2013-12-07T23:18:58.440 回答
0

如果可能,请发布违规行或 SSCE。我同意 jhsheets 和您的方法,根据您的 fx:id 名称仔细检查变量名称。我自己在完全相同的地方遇到了这个确切的错误/情况(在初始化方法中尝试进行一些加载后初始化),我的 fx 中有一个小错字:我导致了 NPE。但是最好知道 NPE 起源的 set 方法中发生了什么。

于 2013-12-08T03:43:50.287 回答