TableView = null, хотя он определен через .fxml

В общем, получаю из MySQL информацию (сразу говорю, с SQL - всё чисто) и хочу передать, назначить их этой таблице, но выдает ошибку:

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
...
Caused by: javafx.fxml.LoadException: 
/C:/Users/.../....fxml
...
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.TableView.setItems(javafx.collections.ObservableList)" because "this.tableView" is null

Я так понимаю ошибка из-за того что TableView = null, но на вопрос почему - ответ не найден.

Вызов и добавление этого ArchorePane:

@FXML
    void managerBooksPageAction(MouseEvent event) throws IOException {
        root = FXMLLoader.load(getClass().getResource("booksManagerPage.fxml"));
        contentAreaPane.setCenter(root);
    }

.fxml:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="840.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.company.librarymanagementsystem.booksManagerPage">
   <children>
      <VBox layoutX="35.0" layoutY="115.0" prefHeight="392.0" prefWidth="157.0" spacing="15.0">
         <children>
            <TextField fx:id="isbnField" prefHeight="10.0" prefWidth="172.0" promptText="ISBN" />
            <TextField fx:id="titleField" prefHeight="10.0" prefWidth="172.0" promptText="Title" />
            <TextField fx:id="languageField" prefHeight="10.0" prefWidth="172.0" promptText="Language" />
            <TextField fx:id="yearField" prefHeight="10.0" prefWidth="172.0" promptText="Year" />
            <TextField fx:id="pagesField" prefHeight="25.0" prefWidth="109.0" promptText="Pages" />
            <ComboBox fx:id="authorComboBox" prefHeight="25.0" prefWidth="197.0" promptText="Author" />
            <ComboBox fx:id="publisherComboBox" prefHeight="25.0" prefWidth="172.0" promptText="Publisher" />
            <ComboBox fx:id="genreComboBox" prefHeight="25.0" prefWidth="161.0" promptText="Genre" />
            <TextArea fx:id="descriptionField" prefHeight="200.0" prefWidth="200.0" promptText="Description" />
         </children>
      </VBox>
      <Button layoutX="35.0" layoutY="535.0" mnemonicParsing="false" onMouseClicked="#addButtonAction" text="Add" />
      <Button layoutX="73.0" layoutY="535.0" mnemonicParsing="false" onMouseClicked="#upgradeButtonAction" text="Upgrade" />
      <Button layoutX="135.0" layoutY="535.0" mnemonicParsing="false" onMouseClicked="#deleteButtonAction" prefHeight="25.0" prefWidth="51.0" text="Delete" />
      <TableView fx:id="tableVIew" layoutX="210.0" layoutY="115.0" prefHeight="392.0" prefWidth="608.0">
        <columns>
          <TableColumn fx:id="idTableColumn" prefWidth="42.0" text="ID" />
          <TableColumn fx:id="isbnTableColumn" prefWidth="71.0" text="ISBN" />
            <TableColumn fx:id="titleTableColumn" prefWidth="65.0" text="Title" />
            <TableColumn fx:id="authorTableColumn" prefWidth="56.0" text="Author" />
            <TableColumn fx:id="publisherTableColumn" prefWidth="62.0" text="Publisher" />
            <TableColumn fx:id="genreTableColumn" prefWidth="54.0" text="Genre" />
            <TableColumn fx:id="pagesTableColumn" minWidth="0.0" prefWidth="55.0" text="Pages" />
            <TableColumn fx:id="yearTableColumn" minWidth="0.0" prefWidth="39.0" text="Year" />
            <TableColumn fx:id="descriptionTableColumn" minWidth="0.0" prefWidth="96.0" text="Description" />
            <TableColumn fx:id="copiesTableColumn" minWidth="0.0" prefWidth="55.0" text="Copies" />
        </columns>
      </TableView>
      <TextField fx:id="keywordsField" layoutX="210.0" layoutY="78.0" prefHeight="25.0" prefWidth="608.0" promptText="Search Books" />
   </children>
</AnchorPane>

booksManagerPage.class:

package com.company.librarymanagementsystem;

import javafx.beans.property.Property;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;

import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class booksManagerPage implements Initializable {

    @FXML
    private TextField isbnField;

    @FXML
    private TextField titleField;

    @FXML
    private TextField languageField;

    @FXML
    private TextField yearField;

    @FXML
    private TextField pagesField;

    @FXML
    private ComboBox<?> authorComboBox;

    @FXML
    private ComboBox<?> publisherComboBox;

    @FXML
    private ComboBox<?> genreComboBox;

    @FXML
    private TextArea descriptionField;

    @FXML
    private TableView<book> tableView;

    @FXML
    private TableColumn<book, Integer> idTableColumn;

    @FXML
    private TableColumn<book, Integer> isbnTableColumn;

    @FXML
    private TableColumn<book, Integer> titleTableColumn;

    @FXML
    private TableColumn<book, Integer> authorTableColumn;

    @FXML
    private TableColumn<book, String> publisherTableColumn;

    @FXML
    private TableColumn<book, String> genreTableColumn;

    @FXML
    private TableColumn<book, Integer> pagesTableColumn;

    @FXML
    private TableColumn<book, Integer> yearTableColumn;

    @FXML
    private TableColumn<book, String> descriptionTableColumn;

    @FXML
    private TableColumn<book, Integer> copiesTableColumn;

    @FXML
    private TextField keywordsField;

    ObservableList<book> bookObservableList = FXCollections.observableArrayList();

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        String bookSearchSQL = "SELECT idBooks, ISBN, title, idAuthor, idPublisher, genre, pages, year, description FROM books";
        try {
            Connection connection = new dbConnection().getDbConnection();
            Statement statement = connection.createStatement();
            ResultSet queryOutput = statement.executeQuery(bookSearchSQL);

            while (queryOutput.next()) {
                Integer queryId = queryOutput.getInt("idBooks");
                Integer queryISBN = queryOutput.getInt("ISBN");
                String queryTitle = queryOutput.getString("title");
                Integer queryIdAuthor = queryOutput.getInt("idAuthor");
                Integer queryIdPublisher = queryOutput.getInt("idPublisher");
                String queryGenre = queryOutput.getString("genre");
                Integer queryPages = queryOutput.getInt("pages");
                Integer queryYear = queryOutput.getInt("year");
                String queryDescription = queryOutput.getString("description");

                bookObservableList.add(new book(queryId, queryISBN, queryTitle, queryIdAuthor, queryIdPublisher, queryGenre, queryPages, queryYear, queryDescription));
            }

            idTableColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
            isbnTableColumn.setCellValueFactory(new PropertyValueFactory<>("ISBN"));
            titleTableColumn.setCellValueFactory(new PropertyValueFactory<>("title"));
            authorTableColumn.setCellValueFactory(new PropertyValueFactory<>("idAuthor"));
            publisherTableColumn.setCellValueFactory(new PropertyValueFactory<>("idPublisher"));
            genreTableColumn.setCellValueFactory(new PropertyValueFactory<>("genre"));
            pagesTableColumn.setCellValueFactory(new PropertyValueFactory<>("pages"));
            yearTableColumn.setCellValueFactory(new PropertyValueFactory<>("year"));
            descriptionTableColumn.setCellValueFactory(new PropertyValueFactory<>("description"));

            tableView.setItems(bookObservableList);

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @FXML
    void addButtonAction(MouseEvent event) {

    }

    @FXML
    void deleteButtonAction(MouseEvent event) {

    }

    @FXML
    void upgradeButtonAction(MouseEvent event) {

    }
}

Ответы (1 шт):

Автор решения: Вадим

В разметке id указан как tableVIew, замените на tableView Так как в контроллере у вас поле имеет имя tableView. Связывание происходит по согласованию имен.

→ Ссылка