我有一个问题,这很难解释,但我会尽最大努力以最好的方式来描述它。我的项目中有这个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);
}
}
控制器:
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);
}
}
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>
所以当我使用类弹出窗口中的open方法从LuggageMenuScherm.FXML切换到Editluggage.FXML以及所有参数时,一旦我尝试更改TextField等项目,它就会崩溃例如setText()。只要我在控制器的editSelected方法中删除editWeight.setText(“sdfsfsdf”),场景就会弹出并运行。我很感激帮助尝试解决此问题,以便我可以在FXML页面上设置项目的文本。两个场景都使用相同的控制器。
答案 0 :(得分:0)
似乎editWeight
尚未初始化。我猜您当前的场景是LuggageMenuScherm.FXML
,因为在editSelected()
方法中代码将加载Editluggage.FXML
。但是,在LuggageMenuScherm.FXML
中,没有ID为editWeight
的组件。
顺便说一句,我建议不要使用一个共同的控制器。
更新
删除fx:controller
中的Editluggage.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
在我的Application类实现中,有一个用于改变场景的方法。可能还有其他更好的方法可以做到这一点。
public void changeScene(Scene scene, String title) {
primaryStage.setScene(scene);
setTitle(title);
primaryStage.sizeToScene();
primaryStage.show();
}