我有一个问题,很难解释,但我会尽力以最好的方式描述它。我的项目中有这个 FXML(场景)(请参阅 Luggage overview )。当我单击“编辑选定的行李”时,它会打开一个新的 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);
}
}
Controller :
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>
行李菜单Scherm.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 方法以及所有参数从 LuggageMenuScherm.FXML 切换到 Editluggage.FXML 时,一旦我尝试使用 setText() 更改 TextField 等项目,它就会崩溃。一旦我删除 Controller 中 editSelected 方法中的 editWeight.setText("sdfsfsdf") ,场景就会弹出并运行。我希望能够帮助您解决此问题,以便我可以在该 FXML 页面上设置项目的文本。此外,两个场景都使用相同 Controller 。
最佳答案
editWeight
似乎尚未初始化。我猜您当前的场景是 LuggageMenuScherm.FXML
,因为在 editSelected()
方法中,代码将加载 Editluggage.FXML
。但是,在 LuggageMenuScherm.FXML
中,没有 id 为 editWeight
的组件。
顺便说一句,我建议不要共用一个 Controller 。
<小时/>更新
删除Editluggage.FXML
中的fx:controller
属性,并尝试以下代码。
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
在我的Application类的实现中,有一个用于更改场景的方法。可能还有其他更好的方法来做到这一点。
public void changeScene(Scene scene, String title) {
primaryStage.setScene(scene);
setTitle(title);
primaryStage.sizeToScene();
primaryStage.show();
}
关于加载场景时 JavaFX 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41048845/