加载场景时发生JavaFX错误

加载场景时发生JavaFX错误,java,javafx,Java,Javafx,我有一个问题,很难解释,但我会尽我最大的努力以最好的方式描述它。我的项目中有这个FXML(场景)(请参阅)。当我点击“编辑所选行李”时,它会打开一个新的FXML文件并显示为弹出窗口。但是,由于某些奇怪的原因,当我尝试使用.setText或其他任何东西来更改窗口中显示的输入字段时,单击“编辑选定的行李”按钮后,会出现以下错误: Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.

我有一个问题,很难解释,但我会尽我最大的努力以最好的方式描述它。我的项目中有这个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);
    }
}
package fastenyourseat.view;
进口FastenYourSeat.dao.luggageDao;
导入FastenYourSeat.helper.changeScene;
导入FastenYourSeat.helper.popup;
进口FastenYourSeat.model.Luggage;
导入java.net.URL;
导入java.util.Collection;
导入java.util.ResourceBundle;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.control.Button;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.TextField;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.input.MouseEvent;
公共类luggageController实现可初始化{
@FXML
按钮试验;
@FXML
TableView AllLuggageView;
@FXML
表列航班号;
@FXML
表列名;
@FXML
TableColumn lastName;
@FXML
表列颜色名称;
@FXML
表列品牌名称;
@FXML
表柱重量;
@FXML
表列状态;
@FXML
表列注释;
@FXML
行李按钮;
@FXML
按钮式行李;
@FXML
TextField编辑权重;
luggageDao luggageDao=新的luggageDao();
行李=新行李();
changeScene场景=新的changeScene();
genericController generic=新的genericController();
@凌驾
公共void初始化(URL位置、ResourceBundle资源){
}
公共空间移除{
System.out.print(baggage.getFlightNr());
}
公共行李已选定(){
行李箱selectedItem=AllLuggageView.getSelectionModel().getSelectedItem();
返回selectedItem;
}
popup popup=新的popup();
公共无效loadLuggageTable(){
Collection-luggageList=luggageDao.getAllLuggage();
setCellValueFactory(新属性ValueFactory(“flightNumber”);
flightNumber.setMinWidth(100);
firstName.setCellValueFactory(新的PropertyValueFactory(“travelerFirstName”));
firstName.setMinWidth(100);
setCellValueFactory(新属性ValueFactory(“travelerLastName”);
lastName.setMinWidth(100);
colorName.setCellValueFactory(新属性ValueFactory(“colorName”));
colorName.setMinWidth(50);
brandName.setCellValueFactory(新属性ValueFactory(“brandName”));
brandName.setMinWidth(100);
重量。setCellValueFactory(新属性ValueFactory(“重量”));
重量。设置最小宽度(100);
state.setCellValueFactory(新属性ValueFactory(“状态”);
状态。设置最小宽度(100);
comment.setCellValueFactory(新属性ValueFactory(“comment”);
注释:设置最小宽度(100);
AllLuggageView.getItems().setAll(luggageList);
}
已选定的公共无效编辑器(){
editwweight.setText(“sdfsdf”);
打开(“EditLuggage.fxml”,“Edit luggage”,this.getClass(),EditLuggage);
}
公共无效关闭弹出窗口(){
弹出。关闭(测试);
}
@FXML
公共作废返回(MouseeEvent事件){
changeSecene(事件,this.getClass(),generic.getPreviousScreen(),changeSecene.Action.BACK);
}
}
Editluggage.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>


因此,当我使用带有所有参数的类Popup中的open方法从luggageneuscherm.FXML切换到Editluggage.FXML时,当我尝试使用例如setText()更改文本字段之类的项时,它就会崩溃。当我在控制器的editSelected方法中删除editWeight.setText(“sdfsfsdf”)时,场景就会弹出并工作。我希望您能帮助我解决这个问题,这样我就可以在FXML页面上设置项目的文本。另外,两个场景都使用相同的控制器。

似乎
editWeight
尚未初始化。我猜您当前的场景是
luggageneuscherm.FXML
,因为在
editSelected()
方法中,代码将加载
editlagage.FXML
。但是,在
luggageneuscherm.FXML
中,没有id为
editWeight
的组件


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


更新

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

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

更新2

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

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

似乎
editWeight
尚未初始化。我猜您当前的场景是
luggageneuscherm.FXML
,因为在
editSelected()
方法中,代码将加载
editlagage.FXML
。但是,在
luggageneuscherm.FXML
中,没有id为
editWeight
的组件


顺便说一句,我建议你不要
public void changeScene(Scene scene, String title) {
    primaryStage.setScene(scene);
    setTitle(title);
    primaryStage.sizeToScene();
    primaryStage.show();
}