Text.setText()不更新文本,抛出NullPointerException [英] Text.setText() not updating text, throws NullPointerException
问题描述
我在使用 Text.setText()
方法时遇到了一些麻烦。它应该将 String
名称设置为它的值。
I'm having a bit of trouble with the Text.setText()
method. It is supposed to set the String
name as it's value.
FXML代码的文字为:
The FXML code has the text:
<Text fx:id="airlineName" strokeType="OUTSIDE" strokeWidth="0.0" text="Airline Name Goes Here" GridPane.rowIndex="1">
<font>
<Font size="17.0" />
</font>
</Text>
这是java代码的一个片段:
Here is a fragment of the java code:
@FXML
Text airlineName;
private void loadNewWindow(String resource, Button button) throws IOException {
Stage stage = (Stage) button.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource(resource));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@FXML
public void viewAirline() throws IOException {
loadNewWindow("/main/resources/Airline Page.fxml", viewAirline);
//some code here, involves Object selected
String name = selected.name;
airlineName.setText(name); //This is line 67
airlineName.setFill(Color.RED);
System.out.println(airlineName.getText());
}
错误消息:
Caused by: java.lang.reflect.InvocationTargetException
...
javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 45 more
Caused by: java.lang.NullPointerException
at main.GUI.Controller.viewAirline(Controller.java:67)
viewAirline
方法是不是在java中使用,而是在FXML中使用:
The viewAirline
method was not used in java, but in FXML:
<Button fx:id="viewAirline" mnemonicParsing="false" onAction="#viewAirline" text="View Airline" GridPane.columnIndex="1" />
每当按钮(类似名称) viewAirline <时,它应该被激活按下/ code>。
It is supposed to be activated whenever a button (similarly named) viewAirline
is pressed.
此FXML代码是 viewAirline()
方法中引用的不同FXML代码(/ main / resources /航空公司Page.fxml)。 文本
元素在 Airline Page.fxml
中引用。所有FXML文件都使用相同的控制器。
This FXML code is a different FXML code referenced in the viewAirline()
method ("/main/resources/Airline Page.fxml"). The Text
element is referenced in the Airline Page.fxml
. All FXML files use the same controller.
推荐答案
如果使用 fx:controller $ c在fxml文件中指定控制器类$ c>属性,
FXMLLoader
在加载期间创建控制器的新实例。这导致 airlineName
为 null
,因为该字段被注入不同的实例控制器类。要使用相同的控制器实例,您需要在加载fxml文件之前设置控制器,并从fxml文件中删除 fx:controller
属性:
If you specify the controller class in the fxml file using the fx:controller
attribute, the FXMLLoader
creates a new instance of the controller during the loading. This leads to the airlineName
being null
since that field is injected to a different instance of the controller class. To use the same controller instance, you need to set the controller before loading the fxml file and also remove the fx:controller
attribute from the fxml file:
FXMLLoader loader = new FXMLLoader(fxmlURL);
loader.setController(controllerInstance);
Parent root = loader.load();
但我建议不要为多个fxmls使用相同的控制器。这导致高耦合和控制器中的更改或一个fxml文件可能会破坏其他fxmls中的某些内容。特别是 initialize
方法需要考虑所有fxmls。
However I wouldn't recommend using the same controller for multiple fxmls. This leads to a high coupling and changes in the controller or one fxml files may break something in the other fxmls. Especially the initialize
method would need to take all fxmls into account.
你最好使用不同的控制器并让它们进行通信。您甚至可以针对接口进行编程,以避免依赖于具体的控制器类。
You better use different controllers and make them communicate. You can even program against an interface to avoid relying on a concrete controller class.
您可以获取由 FXMLLoader创建的控制器实例$ c使用
getController调用
: load()
后$ c>( fx:controller
属性存在) )
You can get the controller instance created by FXMLLoader
(fx:controller
attribute present) after calling load()
using getController()
:
FXMLLoader loader = new FXMLLoader(fxmlURL);
Parent root = loader.load();
ControllerClass controller = loader.getController();
这篇关于Text.setText()不更新文本,抛出NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!