当前位置: 首页 > 知识库问答 >
问题:

加载场景时发生JavaFX错误

刁瀚昂
2023-03-14

我有一个问题,很难解释,但我会尽我最大的努力以最好的方式描述它。我的项目中有这个FXML(场景)(请参见行李概述)。当我点击“编辑所选行李”时,它会打开一个新的FXML文件并显示为弹出窗口。然而,由于一些奇怪的原因,当我尝试使用。setText或其他任何内容单击“编辑选定行李”按钮后,若要更改窗口上显示的输入字段,则会出现以下错误:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    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.reflect.InvocationTargetException
    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.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    ... 31 more
Caused by: java.lang.NullPointerException
    at fastenyourseatbelt.view.luggageController.editSelected(luggageController.java:102)
    ... 41 more

这是我用来使场景弹出的方法:

public class popup {

    public void open(String scene, String title, Class className, Button btn) {
        try {
            Stage stage;
            Parent root;

            stage = new Stage();
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(className.getResource(scene));

            root = loader.load();
            stage.setScene(new Scene(root));
            stage.setTitle(title);
            stage.initModality(Modality.WINDOW_MODAL);
            stage.initOwner(btn.getScene().getWindow());
            stage.showAndWait();
        } catch (IOException ex) {
            Logger.getLogger(luggageController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

控制器:

package fastenyourseatbelt.view;

import fastenyourseatbelt.dao.luggageDao;
import fastenyourseatbelt.helper.changeScene;
import fastenyourseatbelt.helper.popup;
import fastenyourseatbelt.model.Luggage;
import java.net.URL;
import java.util.Collection;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;

public class luggageController implements Initializable {

    @FXML
    Button test;
    @FXML
    TableView<Luggage> AllLuggageView;
    @FXML
    TableColumn<Luggage, String> flightNumber;
    @FXML
    TableColumn<Luggage, String> firstName;
    @FXML
    TableColumn<Luggage, String> lastName;
    @FXML
    TableColumn<Luggage, String> colorName;
    @FXML
    TableColumn<Luggage, String> brandName;
    @FXML
    TableColumn<Luggage, String> weight;
    @FXML
    TableColumn<Luggage, String> state;
    @FXML
    TableColumn<Luggage, String> comment;

    @FXML
    Button deleteLuggage;
    @FXML
    Button editLuggage;
    @FXML
    TextField editWeight;

    luggageDao luggageDao = new luggageDao();
    Luggage luggage = new Luggage();
    changeScene scene = new changeScene();
    genericController generic = new genericController();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }

    public void removeLuggage() {
        System.out.print(luggage.getFlightNr());

    }

    public Luggage getSelected() {
        Luggage selectedItem = AllLuggageView.getSelectionModel().getSelectedItem();
        return selectedItem;
    }
    popup popup = new popup();

    public void loadLuggageTable() {
        Collection luggageList = luggageDao.getAllLuggage();
        flightNumber.setCellValueFactory(new PropertyValueFactory<>("flightNumber"));
        flightNumber.setMinWidth(100);
        firstName.setCellValueFactory(new PropertyValueFactory<>("travelerFirstName"));
        firstName.setMinWidth(100);
        lastName.setCellValueFactory(new PropertyValueFactory<>("travelerLastName"));
        lastName.setMinWidth(100);
        colorName.setCellValueFactory(new PropertyValueFactory<>("colorName"));
        colorName.setMinWidth(50);
        brandName.setCellValueFactory(new PropertyValueFactory<>("brandName"));
        brandName.setMinWidth(100);
        weight.setCellValueFactory(new PropertyValueFactory<>("weight"));
        weight.setMinWidth(100);
        state.setCellValueFactory(new PropertyValueFactory<>("Status"));
        state.setMinWidth(100);
        comment.setCellValueFactory(new PropertyValueFactory<>("comment"));
        comment.setMinWidth(100);
        AllLuggageView.getItems().setAll(luggageList);
    }

    public void editSelected() {

        editWeight.setText("sdfsfsdf");
        popup.open("EditLuggage.fxml", "Edit luggage", this.getClass(), editLuggage);

    }

    public void closePopup() {
        popup.close(test);
    }

    @FXML
    public void back(MouseEvent event) {
        scene.changeScene(event, this.getClass(), generic.getPreviousScreen(), changeScene.Action.BACK);
    }
}

我的行李。FXML

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

    <?import javafx.scene.image.*?>
    <?import javafx.scene.text.*?>
    <?import javafx.scene.control.*?>
    <?import java.lang.*?>
    <?import javafx.scene.layout.*?>

    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="634.0" prefWidth="1037.0" style="-fx-background-color: #ffffff;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fastenyourseatbelt.view.luggageController">
       <children>
          <ChoiceBox fx:id="editFlightnumber" layoutX="297.0" layoutY="202.0" prefHeight="26.0" prefWidth="163.0" />
          <Text layoutX="183.0" layoutY="267.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Size" />
          <Text layoutX="184.0" layoutY="314.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Weight" />
          <TextField id="editWeight" fx:id="editWeight" layoutX="297.0" layoutY="297.0" prefHeight="25.0" prefWidth="163.0" text="ddd" />
          <Text layoutX="183.0" layoutY="220.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Flight number" />
          <Button layoutX="556.0" layoutY="452.0" mnemonicParsing="false" style="-fx-background-color: red;" text="Save" textFill="WHITE" />
          <Button layoutX="441.0" layoutY="452.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="48.0" style="-fx-background-color: blue;" text="Exit" textFill="WHITE" />
          <ImageView fitHeight="119.0" fitWidth="114.0" layoutX="462.0" layoutY="35.0">
             <image>
                <Image url="@../Downloads/user-group-add-512.png" />
             </image>
          </ImageView>
          <Text fx:id="displayTo" layoutX="713.0" layoutY="220.0" strokeType="OUTSIDE" strokeWidth="0.0" text="To" />
          <Text fx:id="displayFrom" layoutX="540.0" layoutY="220.0" strokeType="OUTSIDE" strokeWidth="0.0" text="From" />
          <ChoiceBox fx:id="editSize" layoutX="297.0" layoutY="249.0" prefHeight="26.0" prefWidth="163.0" />
          <ChoiceBox fx:id="editBrand" layoutX="297.0" layoutY="338.0" prefHeight="26.0" prefWidth="163.0" />
          <Text layoutX="185.0" layoutY="355.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Brand" />
          <Text layoutX="184.0" layoutY="398.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Color" />
          <ChoiceBox fx:id="editColor" layoutX="296.0" layoutY="381.0" prefHeight="26.0" prefWidth="163.0" />
          <TextArea fx:id="editComment" layoutX="540.0" layoutY="331.0" prefHeight="86.0" prefWidth="316.0" />
       </children>
    </AnchorPane>

LuggageMenuScherm.FXML

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

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

<AnchorPane prefHeight="720.0" prefWidth="1280.0" style="-fx-background-color: #ffffff;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fastenyourseatbelt.view.luggageController">
   <children>
      <Button id="lostLuggage" fx:id="lostLuggage" layoutX="121.0" layoutY="103.0" mnemonicParsing="false" text="New Lost Luggage" />
      <Button id="foundLuggage" fx:id="foundLuggage" layoutX="272.0" layoutY="103.0" mnemonicParsing="false"  text="New Found Luggage" />
      <Button id="backButton" fx:id="backButton" layoutX="20.0" layoutY="16.0" mnemonicParsing="false" onMouseClicked="#back" text="Back" />
      <TableView id="AllLuggageView" fx:id="AllLuggageView" layoutX="121.0" layoutY="158.0" prefHeight="506.0" prefWidth="1039.0">
         <columns>
            <TableColumn fx:id="flightNumber" prefWidth="75.0" text="Flight number" />
            <TableColumn fx:id="firstName" prefWidth="75.0" text="First name" />
            <TableColumn fx:id="lastName" prefWidth="75.0" text="Last name" />
            <TableColumn fx:id="colorName" prefWidth="75.0" text="Color" />
            <TableColumn fx:id="brandName" prefWidth="75.0" text="Brand" />
            <TableColumn fx:id="weight" prefWidth="75.0" text="Weight" />
            <TableColumn fx:id="state" prefWidth="75.0" text="State" />
            <TableColumn fx:id="comment" prefWidth="75.0" text="Comment" />
         </columns>
      </TableView>
      <Button id="editLuggage" fx:id="editLuggage" layoutX="1030.0" layoutY="122.0" mnemonicParsing="false" onMouseClicked="#editSelected" text="Edit selected luggage" />
      <Button id="deleteLuggage" fx:id="deleteLuggage" layoutX="120.0" layoutY="55.0" mnemonicParsing="false" onAction="#loadLuggageTable" onMouseClicked="#loadLuggageTable" text="Fetch ALL Luggage" />
      <Button id="editLuggage" fx:id="editLuggage1" layoutX="1007.0" layoutY="80.0" mnemonicParsing="false" onMouseClicked="#editSelected" text="Remove selected luggage" />
   </children>
</AnchorPane>

所以当我从行李间切换时会发生什么。将FXML发送到Editluggage。FXML使用类Popup中的open方法以及所有参数,当我尝试使用例如setText()更改TextField之类的项时,FXML就会崩溃。只要我取下重量。setText(“SDFSDF”)在控制器中的editSelected方法中,场景弹出并工作。我希望您能帮助我解决这个问题,这样我就可以在FXML页面上设置项目的文本。两个场景也使用相同的控制器。

共有1个答案

万俟丁雷
2023-03-14

似乎editWeight没有初始化。我猜你现在的场景是LuggageMenuScherm。FXML,因为在editS选()方法中,代码将加载Edit行李。FXML.但是,在LuggageMenuScherm中。FXML,没有id是editWeight的组件。

顺便说一下,我建议不要共用一个控制器。

使现代化

删除Editluggage中的fx:controller属性。FXML,然后尝试以下代码

FXMLLoader loader = new FXMLLoader(<FXML file>);
loader.setController(this);
Parent node = loader.load();

public void open(String scene, String title, Class className, Object controller, Button btn) {
    // Add a controller param
    try {
        Stage stage;
        Parent root;

        stage = new Stage();
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(className.getResource(scene));
        loader.setController(controller); // Set default controller
        root = loader.load();
        stage.setScene(new Scene(root));
        stage.setTitle(title);
        stage.initModality(Modality.WINDOW_MODAL);
        stage.initOwner(btn.getScene().getWindow());
        stage.showAndWait();
    } catch (IOException ex) {
        Logger.getLogger(luggageController.class.getName()).log(Level.SEVERE, null, ex);
    }

更新2

在我的应用程序类实现中,有一个用于更改场景的方法。也许还有其他更好的方法可以做到这一点。

public void changeScene(Scene scene, String title) {
    primaryStage.setScene(scene);
    setTitle(title);
    primaryStage.sizeToScene();
    primaryStage.show();
}

 类似资料:
  • 我希望此图表显示在场景生成器上。怎么可能。。??

  • 我对屏幕生成器有一些问题 对于某些fxml,它会在加载后发生creash。我尝试了很多次运行它,但它阻塞了,我必须终止这个进程。 fxml正常,内容在我的应用程序中正确显示。下面是导致场景生成器崩溃的FXML示例。

  • 问题内容: 我正在构建JavaFX应用程序,我想知道是否有关于如何尽快加载当前新内容的建议(最佳实践)。 当前我正在做的(或多或少)是这样的: 对于加载简单的s,es等的复杂场景而言,上面的方法对于简单的s BUT 来说足够好且快速。 我在的方法中进行的所有初始化。 在那里,我将项目添加到框中,进行初始化等,但是正如我所说的,这花费了太多时间。 难道我做错了什么?我应该在其他地方初始化吗? 谢谢。

  • 问题内容: 我有一个使用javafx Scene来渲染某些东西的应用程序,并且我想将该渲染结果放入我在Javafx中创建的某些GUI中。我该怎么做? 基本上,有一些容器可以放入场景,然后将其放入GUI。 抱歉,如果是新手问题,我是JavaFX的新手 问题答案: 该场景只有一个顶级父节点作为根。您可以获取它并放入另一个场景。

  • 我有下面的FXML,它是我在另一个场景中按下按钮时加载的 代码是: 如果我这样做,一切都显示得很好。但我想在这个场景中添加一个带有动态消息的标签,并且仍然保留fxml中的所有内容。如果我这样做,其中消息是字符串 在< code>setScene之前,它将添加我的标签,但不添加来自fxml的元素。 有什么建议吗?谢谢

  • 我创建了一个游戏,我想给它添加一个开始屏幕,我使用FXML添加了它,还添加了两个按钮(开始和退出)。 按下开始按钮后,我希望游戏加载场景并切换到游戏开始。我对如何做有一个粗略的想法,但我有点挣扎,因为我的SampleController类不知道如何启动游戏等,因为所有代码(以及加载初始开始菜单的代码)都在我的主类中,所以我尝试了这样的事情: 我尝试使用一个函数来切换场景,但它不起作用,也试图使用获