label.setText() 时出现 NullPointerException

NullPointerException occured with label.setText()

我正在尝试使用 setText() 方法将文本设置为不同的标签。我确实尝试打印该值以检查 setText 是否收到 null。但是我能够在控制台中成功打印该值,但我的问题是为什么不能将其设置为标签。

任何建议都有助于解决此 NullPointerException。

PersonOverviewController.java

package passion.controllers;
import passion.models.Person;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

public class PersonOverviewController{
    @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;

    public PersonOverviewController() {

    }

    @FXML
    private void initialize() {
        // Initialize the person table with the two columns.
        firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
        lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
        try{
            //showPersonDetails(null);
            //personTable.getSelectionModel().selectedItemProperty().addListener( (observable, oldValue, newValue) -> showPersonDetails(newValue));
            personTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Person>(){
                @Override
                public void changed(ObservableValue<? extends Person> observable, Person oldValue, Person newValue) {
                    System.out.println(newValue);
                    showPersonDetails(newValue);
                }
            });  
        }catch(NullPointerException e){
            e.printStackTrace();
        }
    }

    public void showTableViewData(MainApp mainApp) {
        personTable.setItems(mainApp.getPersonData());
    }

    public void showPersonDetails(Person person){
        Platform.runLater(new Runnable(){
            @Override
            public void run() {
                if(person != null){
                    System.out.println(person.getFirstName());
                    firstNameLabel.setText(person.getFirstName());
                    System.out.println("test");
                    lastNameLabel.setText(person.getLastName());
                    streetLabel.setText(person.getStreet());
                    postalCodeLabel.setText(Integer.toString(person.getPostalCode()));
                    cityLabel.setText(person.getCity());
                    birthdayLabel.setText(person.getBirthday().toString());
                }else{
                    firstNameLabel.setText("");
                    lastNameLabel.setText("");
                    streetLabel.setText("");
                    postalCodeLabel.setText("");
                    cityLabel.setText("");
                    birthdayLabel.setText("");
                }
            }
        });
    }
}

这就是我从 MainApp.java

加载控制器的方式
public void showPersonOverview(){
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("/passion/viewControllers/PersonOverView.fxml"));

        AnchorPane personOverview = (AnchorPane)loader.load();
        root.setCenter(personOverview);

        PersonOverviewController personController = loader.getController();
        personController.showTableViewData(this);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

PersonOverview.fxml

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

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


<AnchorPane prefHeight="300.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="passion.controllers.PersonOverviewController">
   <children>
      <SplitPane dividerPositions="0.29797979797979796" layoutX="200.0" layoutY="70.0" prefHeight="300.0" prefWidth="600.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 fx:id="personTable" layoutX="-12.0" layoutY="49.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="-0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <columns>
                      <TableColumn fx:id="firstNameColumn" prefWidth="75.0" text="First Name" />
                      <TableColumn fx:id="lastNameColumn" prefWidth="75.0" text="Last Name" />
                    </columns>
                     <columnResizePolicy>
                        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                     </columnResizePolicy>
                  </TableView>
               </children>
            </AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <Label layoutX="14.0" layoutY="14.0" text="Person Details:" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="5.0" />
                  <GridPane layoutX="34.0" layoutY="38.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="30.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">
                           <GridPane.margin>
                              <Insets left="5.0" />
                           </GridPane.margin>
                        </Label>
                        <Label text="Last Name" GridPane.rowIndex="1">
                           <GridPane.margin>
                              <Insets left="5.0" />
                           </GridPane.margin>
                        </Label>
                        <Label text="Street" GridPane.rowIndex="2">
                           <GridPane.margin>
                              <Insets left="5.0" />
                           </GridPane.margin>
                        </Label>
                        <Label text="City" GridPane.rowIndex="3">
                           <padding>
                              <Insets left="5.0" />
                           </padding>
                        </Label>
                        <Label text="Postal Code" GridPane.rowIndex="4">
                           <GridPane.margin>
                              <Insets left="5.0" />
                           </GridPane.margin>
                        </Label>
                        <Label text="Birthday" GridPane.rowIndex="5">
                           <GridPane.margin>
                              <Insets left="5.0" />
                           </GridPane.margin>
                        </Label>
                        <Label fx:id="firstName" text="Label" GridPane.columnIndex="1">
                           <padding>
                              <Insets left="5.0" />
                           </padding>
                        </Label>
                        <Label fx:id="lastName" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1">
                           <GridPane.margin>
                              <Insets left="5.0" />
                           </GridPane.margin>
                        </Label>
                        <Label fx:id="street" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2">
                           <GridPane.margin>
                              <Insets left="5.0" />
                           </GridPane.margin>
                        </Label>
                        <Label fx:id="city" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3">
                           <GridPane.margin>
                              <Insets left="5.0" />
                           </GridPane.margin>
                        </Label>
                        <Label fx:id="postal" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="4">
                           <GridPane.margin>
                              <Insets left="5.0" />
                           </GridPane.margin>
                        </Label>
                        <Label fx:id="birthday" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5">
                           <GridPane.margin>
                              <Insets left="5.0" />
                           </GridPane.margin>
                        </Label>
                     </children>
                  </GridPane>
                  <HBox layoutX="229.0" layoutY="244.0" spacing="5.0" AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="10.0">
                     <children>
                        <Button layoutX="229.0" layoutY="244.0" mnemonicParsing="false" text="New..." />
                        <Button layoutX="289.0" layoutY="244.0" mnemonicParsing="false" text="Edit..." />
                        <Button layoutX="351.0" layoutY="244.0" mnemonicParsing="false" text="Delete" />
                     </children>
                  </HBox>
               </children>
            </AnchorPane>
        </items>
      </SplitPane>
   </children>
</AnchorPane>

控制台:

Person [firstName=StringProperty [value: Satya], lastName=StringProperty [value: Vema], street=StringProperty [value: some street], postalCode=IntegerProperty [value: 1234], city=StringProperty [value: some city], birthday=ObjectProperty [value: 1999-02-21]]
Person [firstName=StringProperty [value: Satya], lastName=StringProperty [value: Vema], street=StringProperty [value: some street], postalCode=IntegerProperty [value: 1234], city=StringProperty [value: some city], birthday=ObjectProperty [value: 1999-02-21]]
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at passion.controllers.PersonOverviewController.run(PersonOverviewController.java:69)
    at com.sun.javafx.application.PlatformImpl.lambda$null0(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda/32693051.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater1(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda/2180324.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null5(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda/3326003.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

缺少连接(fx:id 到变量名称 @FXML@FXML("..."))。

<Label text="First Name" fx:id="firstNameLabel">

因此无法将组件设置为 FXML 生成的对话框的实际组件。

我认为您遗漏了一部分代码。

TextView firstNameLabel = (TextView)findIdById(R.id.yourTextViewIdInLayout);

并且您必须对所有小部件执行此操作,以免获得 NullPointerException