首页 > 解决方案 > 未加载 FXML 文件

问题描述

所以,我有一个 FXML 文件BookView_FXML.fxml和一个控制器BookView. show()FXML 文件在控制器类内的方法中加载。当我运行应用程序时,我得到一个fxml.LoadException.

这是错误报告:

javafx.fxml.LoadException: 
/C:/path_to_package/memberApp/BookView_FXML.fxml:7

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    at javafx.fxml/javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:105)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:941)
    at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
    at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at memberApp.BookView.display(BookView.java:58)
    at memberApp.HomeScreenController.lambda$initializeTableView$1(HomeScreenController.java:181)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3564)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8200(Scene.java:3492)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3860)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.InstantiationException: memberApp.BookView
    at java.base/java.lang.Class.newInstance(Class.java:571)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:936)
    ... 44 more
Caused by: java.lang.NoSuchMethodException: memberApp.BookView.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3349)
    at java.base/java.lang.Class.newInstance(Class.java:556)
    ... 45 more

BookView班级_

package memberApp;

// excluded the import statements

public class BookView implements Initializable
{
    public AnchorPane root ;
    public Label bookIDLbl, titleLbl, authorLbl, isbnLbl, publisherLbl, categoryLbl, copiesAvailableLbl ;
    public VBox vbox ;

    private final Book theBook ;
    private String bookID, title, author, isbn, publisher, category, copiesAvailable ;

    public BookView(Book book)
    {
        this.theBook = book ;
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle)
    {
        initializeData();
        initializeLabels() ;
    }


    public void display()
    {
        Stage primary_stage = new Stage() ;
        primary_stage.initModality(Modality.APPLICATION_MODAL) ;   // the initModality specifies that the stage will block events
        primary_stage.setTitle("BOOK INFO") ;

        try {
            Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("BookView_FXML.fxml"))) ;
            Scene scene = new Scene(root) ;

            primary_stage.setScene(scene) ;
            primary_stage.show() ;
        }
        catch (Exception e){
            System.out.println("\nExceptions in BookView.display() -> " + e) ;
            e.printStackTrace() ;
        }
    }


    private void initializeData()
    {
        bookID = theBook.getBookID() ;
        title = theBook.getTitle() ;
        author = theBook.getAuthor() ;
        isbn = theBook.getIsbn() ;
        publisher = theBook.getPublisher() ;
        category = theBook.getCategory() ;
        copiesAvailable = theBook.getTotalCopies() ;
    }


    private void initializeLabels()
    {
        bookIDLbl.setText("BookID: " + bookID) ;
        titleLbl.setText(title) ;
        authorLbl.setText("BY " + author) ;
        isbnLbl.setText("ISBN: " + isbn) ;
        publisherLbl.setText("Publisher: " + publisher) ;
        categoryLbl.setText("Category: " + category) ;
        copiesAvailableLbl.setText("Copies Available: " + copiesAvailable) ;
    }
}

FXML 文件:

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane fx:id="root" minHeight="-Infinity" minWidth="-Infinity" prefHeight="450.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="memberApp.BookView">
   <Label fx:id="titleLbl" alignment="CENTER" minHeight="-Infinity" minWidth="-Infinity" prefHeight="50.0" text="&lt;title goes here&gt;" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <font>
         <Font size="20.0" />
      </font>
   </Label>
   <Label fx:id="authorLbl" alignment="CENTER" layoutX="10.0" layoutY="10.0" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" text="&lt;author's name goes here&gt;" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="50.0">
      <font>
         <Font size="15.0" />
      </font>
   </Label>
   <VBox fx:id="vbox" alignment="CENTER" layoutX="250.0" layoutY="100.0" prefHeight="200.0" prefWidth="100.0" spacing="10.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="100.0">
      <Label fx:id="bookIDLbl" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="150.0" text="Label">
         <font>
            <Font size="13.5" />
         </font>
      </Label>
      <Label fx:id="isbnLbl" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="150.0" text="Label">
         <font>
            <Font size="13.5" />
         </font>
      </Label>
      <Label fx:id="categoryLbl" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="150.0" text="Label">
         <font>
            <Font size="13.5" />
         </font>
      </Label>
      <Label fx:id="publisherLbl" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="150.0" text="Label">
         <font>
            <Font size="13.5" />
         </font>
      </Label>
      <Label fx:id="copiesAvailableLbl" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="150.0" text="Label">
         <font>
            <Font size="13.5" />
         </font>
      </Label>
   </VBox>
</AnchorPane>

我不确定是什么导致了异常。任何帮助是极大的赞赏。

标签: javaexceptionjavafxfxml

解决方案


推荐阅读