我已经构建了联系人管理器应用程序,当我尝试运行它时出现以下错误
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>
答案 0 :(得分:0)
您的FXML文件使用sap.m.Token
属性,而不是所有控件上的id
属性。因此,fx:id
未对@FXML
个带注释的字段进行初始化。
你需要
FXMLLoader
和所有其他控件类似。