簡體   English   中英

應用初始化時發生FXMLLoader.constructLoadException

[英]FXMLLoader.constructLoadException occuered when app gets initialize

我已經建立了聯絡人管理器應用程序,當我嘗試運行它時,出現以下錯誤

at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at `enter code here`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$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at addressapp.PersonOverviewController.initialize(PersonOverviewController.java:52)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 13 more

之后,我搜索了應用程序停止的位置,並停止在表視圖列的initialize()塊和setCellValueFactory()中。 這是我的控制器代碼。 請告訴我這段代碼是什么問題?

控制者

package addressapp;

import addressapp.Model.Person;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;


public class PersonOverviewController implements Initializable {

    @FXML
    private TableView<Person> personTable;

    @FXML
    private TableColumn<Person, String> firstNameColumn;

    @FXML
    private TableColumn<Person, String> lastNameColumn;

    @FXML
    private Label firstNameLabel;

    @FXML
    private Label lastNameLabel;
    @FXML
    private Label streetLabel;
    @FXML
    private Label postalCodeLabel;
    @FXML
    private Label cityLabel;
    @FXML
    private Label birthdayLabel;

    private AddressApp AddressApp;

    public PersonOverviewController() {
    }



    @Override
    public void initialize(URL url, ResourceBundle rb) {

        firstNameColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));

        lastNameColumn.setCellValueFactory(new PropertyValueFactory<>("lastName"));

          }

    public void setMainApp(AddressApp mainApp) {

        this.AddressApp = mainApp;

        personTable.setItems(mainApp.getPersonData());
    }

}

這是我的FXML

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

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

<AnchorPane id="AnchorPane" prefHeight="484.0" prefWidth="584.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="addressapp.PersonOverviewController">
    <children>
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
      <SplitPane layoutX="-2.0" prefHeight="484.0" prefWidth="584.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
          <AnchorPane prefHeight="300.0" prefWidth="600.0">
               <children>
                  <SplitPane dividerPositions="0.29797979797979796" layoutX="14.0" prefHeight="482.0" prefWidth="582.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <items>
                      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                           <children>
                              <TableView id="personTable" layoutX="-7.0" layoutY="40.0" prefHeight="480.0" prefWidth="170.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                                <columns>
                                  <TableColumn id="firstNameColumn" prefWidth="75.0" text="First Name" />
                                  <TableColumn id="lastNameColumn" prefWidth="83.0" text="Last Name" />
                                </columns>
                                 <columnResizePolicy>

                                 </columnResizePolicy>
                              </TableView>
                           </children>
                        </AnchorPane>
                      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                           <children>
                              <Label layoutX="25.0" layoutY="25.0" text="Person Details" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="5.0" />
                              <GridPane layoutX="14.0" layoutY="42.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="40.0">
                                <columnConstraints>
                                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                </columnConstraints>
                                <rowConstraints>
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                </rowConstraints>
                                 <children>
                                    <Label text="First Name" />
                                    <Label text="Last Name" GridPane.rowIndex="1" />
                                    <Label text="Street" GridPane.rowIndex="2" />
                                    <Label text="City" GridPane.rowIndex="3" />
                                    <Label text="Postal Code" GridPane.rowIndex="4" />
                                    <Label text="Birthday" GridPane.rowIndex="5" />
                                    <Label id="firstNameLabel" text="Label" GridPane.columnIndex="1" />
                                    <Label id="lastNameLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                                    <Label id="streetLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                                    <Label id="postalCodeLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3" />
                                    <Label id="cityLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="4" />
                                    <Label id="birthdayLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5" />
                                 </children>
                              </GridPane>
                              <Button layoutX="338.0" layoutY="441.0" mnemonicParsing="false" text="Delete" />
                              <Button layoutX="286.0" layoutY="441.0" mnemonicParsing="false" text="Edit.." />
                              <Button layoutX="232.0" layoutY="441.0" mnemonicParsing="false" text="New.." AnchorPane.rightAnchor="126.0" />
                           </children>
                        </AnchorPane>
                    </items>
                  </SplitPane>
               </children></AnchorPane>
        </items>
      </SplitPane>
    </children>
</AnchorPane>

您的FXML文件使用的是id屬性,而不是所有控件上的fx:id屬性。 因此,沒有任何的@FXML -annotated字段由初始化FXMLLoader

你需要

<TableColumn fx:id="firstNameColumn" prefWidth="75.0" text="First Name" />
<TableColumn fx:id="lastNameColumn" prefWidth="83.0" text="Last Name" />

對於所有其他控件也是如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM