FXMLLoader.constructLoadException occuered when app gets initialize

半城伤御伤魂 提交于 2019-12-12 03:27:30

问题


I have build contact manager app and When I try to run it I got following error

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

After that I searched where there the application stopped stopped within the initialize() block and setCellValueFactory() of my table view column. This is my controller code. Please tell me what is the matter of this code?

Controller

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());
    }

}

Here my 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>

回答1:


Your FXML file is using an id attribute, instead of the fx:id attribute on all the controls. Consequently, none of the @FXML-annotated fields are initialized by the FXMLLoader.

You need

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

and similarly for all the other controls.



来源:https://stackoverflow.com/questions/36737653/fxmlloader-constructloadexception-occuered-when-app-gets-initialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!