将自己的数据 class 插入表视图时出现 FXML 加载异常

FXML Load exception when inserting data of own class into tableview

我又遇到问题了。 我正在尝试理解 FXML,我目前正在使用 oracle "creating an address book with FXML" 的 "Mastering FXML" 指南。 我已经接近指南的末尾了,但我无法解决自昨天以来遇到的问题。 我想为我父亲写一个维护程序,但我总是得到声明:
javafx.fxml.LoadException: Maintenance is not a valid type.
我猜有 3 个 类 对于这个问题很重要。那将是我的 "mainLayout.fxml"、"FXMLDocumentController.java" 和 "Maintenance.java" 所以我只是 post 按此顺序

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.collections.*?>


<Pane fx:controller="FXMLDocumentController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="858.0" prefWidth="1276.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <SplitPane dividerPositions="0.8687747035573122" layoutX="2.0" layoutY="25.0" prefHeight="839.0" prefWidth="1276.0">
            <items>
                <AnchorPane fx:id="anchorpaneLeft" minHeight="0.0" minWidth="0.0" prefHeight="811.0" prefWidth="1104.0">
                    <children>
                        <ScrollPane id="scrollpane" fx:id="scrollpane" layoutX="452.0" layoutY="319.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                            <content>
                                <TableView id="tableView" fx:id="tableView" prefHeight="834.0" prefWidth="1102.0">
                                    <columns>
                                        <TableColumn text="%tablecolumnName" >
                                            <cellValueFactory><PropertyValueFactory property = "name"/>
                                            </cellValueFactory>
                                        </TableColumn>
                                        <TableColumn text="%tablecolumnAddress" >
                                            <cellValueFactory><PropertyValueFactory property = "address"/>
                                            </cellValueFactory>
                                        </TableColumn>
                                        <TableColumn text="%tablecolumnNumber" >
                                            <cellValueFactory><PropertyValueFactory property = "number"/>
                                            </cellValueFactory>
                                        </TableColumn>
                                        <TableColumn text="%tablecolumnMail" >
                                            <cellValueFactory><PropertyValueFactory property = "mail"/>
                                            </cellValueFactory>
                                        </TableColumn>
                                        <TableColumn text="%tablecolumnLast" >
                                            <cellValueFactory><PropertyValueFactory property = "last"/>
                                            </cellValueFactory>
                                        </TableColumn>
                                        <TableColumn text="%tablecolumnNext" >
                                            <cellValueFactory><PropertyValueFactory property = "next"/>
                                            </cellValueFactory>
                                        </TableColumn>
                                        <TableColumn text="%tablecolumnNote" >
                                            <cellValueFactory><PropertyValueFactory property = "note"/>
                                            </cellValueFactory>
                                        </TableColumn>
                                    </columns>
                                    <items>
                                        <FXCollections fx:factory="observableArrayList">
                                            <Maintenance name="jacob" address="fichtestraße 5" number="01515242" mail="jacob@jacob.com" last="10.10.2018" next="10.10.2019" note="leer jung"/>
                                        </FXCollections>
                                    </items>
                                </TableView>
                            </content>
                        </ScrollPane>
                    </children>
                </AnchorPane>
                <AnchorPane fx:id="anchorpaneRight" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                    <children>
                        <VBox fx:id="vboxNew" alignment="TOP_CENTER" layoutX="50.0" layoutY="52.0" prefHeight="85.0" prefWidth="164.0" spacing="15.0" AnchorPane.bottomAnchor="726.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="26.0">
                            <children>
                                <Label fx:id="labelNew" prefHeight="17.0" prefWidth="192.0" text="%labelNew">
                                    <font>
                                        <Font name="System Bold" size="12.0" />
                                    </font>
                                </Label>
                                <Button fx:id="buttonInsert" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonInsert" onAction="#handleInsertButton" textAlignment="CENTER" />
                            </children>
                        </VBox>
                        <VBox fx:id="vboxEdit" alignment="TOP_CENTER" layoutY="120.0" prefHeight="717.0" prefWidth="164.0" spacing="15.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="120.0">
                            <children>
                                <Label fx:id="labelEdit" prefHeight="17.0" prefWidth="166.0" text="%labelEdit">
                                    <font>
                                        <Font name="System Bold" size="12.0" />
                                    </font>
                                </Label>
                                <Button fx:id="buttonEdit" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonEdit" textAlignment="CENTER" />
                                <Button fx:id="buttonDelete" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonDelete" />
                                <Button fx:id="buttonWeek" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonWeek" />
                                <Button fx:id="buttonMonth" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonMonth" />
                                <Button fx:id="buttonYear" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonYear" />
                            </children>
                        </VBox>
                    </children>
                </AnchorPane>
            </items>
        </SplitPane>
        <MenuBar fx:id="menubar" layoutX="2.0" layoutY="2.0" prefHeight="25.0" prefWidth="1276.0">
            <menus>
                <Menu fx:id="menuFile" mnemonicParsing="false" text="%menuFile">
                    <items>
                        <MenuItem mnemonicParsing="false" text="Close" />
                    </items>
                </Menu>
                <Menu fx:id="menuEdit" mnemonicParsing="false" text="%menuEdit">
                    <items>
                        <MenuItem mnemonicParsing="false" text="Delete" />
                    </items>
                </Menu>
                <Menu fx:id="menuHelp" mnemonicParsing="false" text="%menuHelp">
                    <items>
                        <MenuItem fx:id="itemAbout" mnemonicParsing="false" text="%itemAbout" />
                    </items>
                </Menu>
                <Menu fx:id="menuLanguage" mnemonicParsing="false" text="%menuLanguage">
                    <items>
                        <MenuItem fx:id="itemGer" mnemonicParsing="false" text="%itemGer" />
                        <MenuItem fx:id="itemEng" mnemonicParsing="false" text="%itemEng" />
                    </items>
                </Menu>
            </menus>
        </MenuBar>
    </children>
</Pane>

FXMLDocumentController.java:

import java.net.URL;
import java.util.*;
import javafx.collections.*;
import javafx.event.*;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.*;


public class FXMLDocumentController implements Initializable {

    @FXML private Label label;

    @FXML private TableView<Maintenance> table;

    @FXML private TableColumn tablecolumnName;
    @FXML private TableColumn tablecolumnAddress;
    @FXML private TableColumn tablecolumnNumber;
    @FXML private TableColumn tablecolumnMail;
    @FXML private TableColumn tablecolumnLast;
    @FXML private TableColumn tablecolumnNext;
    @FXML private TableColumn tablecolumnNote;

    @FXML private Button buttonInsert;
    @FXML private Button buttonEdit;
    @FXML private Button buttonDelete;
    @FXML private Button buttonWeek;
    @FXML private Button buttonMonth;
    @FXML private Button buttonYear;

    @FXML private TextField textfieldName;
    @FXML private TextField textfieldAddress;
    @FXML private TextField textfieldMail;
    @FXML private TextField textfieldNumber;
    @FXML private TextField textfieldLast;
    @FXML private TextField textfieldNext;
    @FXML private TextField textfieldNote;

    @FXML private TableView<Maintenance> tableView;


    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

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

    }

    @FXML
    void handleInsertButton(ActionEvent event){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("insertWindow.fxml"));
            Parent root1 = (Parent) fxmlLoader.load();
            Stage stage = new Stage();
            stage.setTitle("Insert Window");
            Scene scene = new Scene (root1);
            stage.setScene(scene);
            stage.show();
        }   catch(Exception e){
            e.printStackTrace();
        }
    }

    protected void handleEditButton() {

    }

    protected void handleDeleteButton() {

    }

    protected void handleWeekButton() {

    }

    protected void handleMonthButton() {

    }

    protected void handleYearButton() {

    }

    protected void handleGerButton() {

    }

    protected void handleEngButton() {

    }

    @FXML 
    protected void addMaintenance(ActionEvent event){
        ObservableList <Maintenance> data = tableView.getItems();
        data.add(new Maintenance(textfieldName.getText(), textfieldAddress.getText(),
                            textfieldMail.getText(), textfieldNumber.getText(),
                            textfieldLast.getText(), textfieldNext.getText(),
                            textfieldNote.getText()));
        textfieldName.setText("");
        textfieldAddress.setText("");
        textfieldMail.setText("");
        textfieldNumber.setText("");
        textfieldLast.setText("");
        textfieldNext.setText("");
        textfieldNote.setText("");

    }


}

最后 Maintenance.java

import javafx.beans.property.SimpleStringProperty;

public class Maintenance {

    private final SimpleStringProperty name = new SimpleStringProperty("");
    private final SimpleStringProperty address = new SimpleStringProperty("");
    private final SimpleStringProperty number = new SimpleStringProperty("");
    private final SimpleStringProperty mail = new SimpleStringProperty("");
    private final SimpleStringProperty last = new SimpleStringProperty("");
    private final SimpleStringProperty next = new SimpleStringProperty("");
    private final SimpleStringProperty note = new SimpleStringProperty("");

    public Maintenance(){
        this("", "", "", "", "", "", "");
    }

    public Maintenance(String name, String address, String number, String mail, String last, String next, String note){
        setName(name);
        setAddress(address);
        setNumber(number);
        setMail(mail);
        setLast(last);
        setNext(next);
        setNote(note);
    }

    public String getName() {
        return name.get();
    }
    public String getAddress() {
        return address.get();
    }
    public String getNumber() {
        return number.get();
    }
    public String getMail() {
        return mail.get();
    }
    public String getLast() {
        return last.get();
    }
    public String getNext() {
        return next.get();
    }
    public String getNote() {
        return note.get();
    }
    public final void setName(String Name){
        name.set(Name);
    }
    public final void setAddress(String Address){
        address.set(Address);
    }
    public final void setNumber(String Number){
        number.set(Number);
    }
    public final void setMail(String Mail){
        mail.set(Mail);
    }
    public final void setLast(String Last){
        last.set(Last);
    }
    public final void setNext(String Next){
        next.set(Next);
    }
    public final void setNote(String Note){
        note.set(Note);
    }

}

insertwindow.fxml 存在并且到目前为止工作得很好所以我不会 post 它来缩短我的 post 相同的 main.java 和我的 4 属性 文件。 希望有人能再次告诉我我做错了什么。 感谢您的帮助!

Executing C:\Users\Schurke\Documents\NetBeansProjects\Maintenance\dist\run365389303\Maintenance.jar using platform D:\Program Files\Java\jdk1.8.0_202\jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication9(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: 
file:/C:/Users/Schurke/Documents/NetBeansProjects/Maintenance/dist/run365389303/Maintenance.jar!/mainLayout.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
    at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
    at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3116)
    at Main.start(Main.java:19)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication16(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait9(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null7(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater8(PlatformImpl.java:294)
    at 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$null2(WinApplication.java:177)
    ... 1 more
Caused by: java.lang.ClassNotFoundException
    at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2899)
    at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
    ... 19 more
Exception running application Main
Java Result: 1

对于自定义 FXML 类型,您应该添加导入语句:

<?import Maintenance?>

一般来说,使用默认包是一种不好的做法。将 Maintenance.java 放入某个包中并将其导入为:

<?import yourPackageName.Maintenance?>