JavaFX,标签空指针异常 [英] JavaFX, Label null pointer exception
问题描述
我目前正在编写的程序遇到以下问题,我在互联网上进行了搜索,但我真的找不到任何可以帮助我理解以下问题的内容
I am having the following problem with a program that I am currently writing, and I have searched on the internet, but I couldn't really find anything to help me understand the following problem
因此在另一个类中,我编写了一个方法,该方法在单击搜索按钮时执行该方法,该方法如下所示:
So inside another class I have written a method that executes this whenever the search button is clicked and the method looks like this:
public void searchButton(){
try {
new SearchController().display();
} catch (IOException e) {
e.printStackTrace();
}
}
然后 SearchController 类看起来像这样(我在这里简化了它):
And then the SearchController class looks something like this (I simplified it here):
public class SearchController {
@FXML
private Button cancelButton;
@FXML
private Label what;
private static Stage stage;
private static BorderPane borderPane;
@FXML
public void initialize(){
what.setText("Testing"); // this woks
cancelButton.setOnAction(e -> stage.close());
}
public void display() throws IOException {
stage = new Stage();
stage.setResizable(false);
stage.setTitle("Product search");
stage.initModality(Modality.APPLICATION_MODAL);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(SearchController.class.getResource("Search.fxml"));
borderPane = loader.load();
Scene scene = new Scene(borderPane);
stage.setScene(scene);
//what.setText("Testing") and this doesn't work
stage.showAndWait();
}
}
谁能告诉我为什么可以在 initialize 方法上写入文本(该方法在 borderPane = loader.load();
行之后被调用...所以为什么不如果我尝试在该行之后的标签上写它会起作用吗?)
Can someone please tell me why it is possible to write text on the initialize method (that method gets called after the borderPane = loader.load();
line...so why doesn't it work if I try to write on the label after that line?)
提前致谢
推荐答案
FXMLLoader
创建在 FXML 根的 fx:controller
属性中指定的类的实例元素.然后,当 fx:id
属性与字段名称匹配时,它会将 FXML 文件中定义的元素注入它创建的控制器实例.然后它调用该实例上的 initialize()
方法.
The FXMLLoader
creates an instance of the class specified in the fx:controller
attribute of the FXML root element. It then injects the elements defined in the FXML file into the controller instance it created when the fx:id
attributes match the field names. Then it calls the initialize()
method on that instance.
您使用new SearchController()
手动"创建控制器的实例.这与 FXMLLoader
创建的对象不同.所以现在当你加载了 fxml 文件时,你有两个不同的 SearchController
实例.因此,如果您从 display()
方法调用 what.setText(...)
,则不会在由 FXMLLoader<创建的控制器实例上调用它/代码>.因此,
what
尚未在您调用 what.setText(...)
的实例中初始化,并且您会收到空指针异常.
You create an instance of the controller "by hand" with new SearchController()
. This is not the same object that is created by the FXMLLoader
. So now when you have loaded the fxml file you have two different instances of SearchController
. So if you call what.setText(...)
from the display()
method, you are not calling it on the controller instance created by the FXMLLoader
. Consequently, what
has not been initialized in the instance on which you are calling what.setText(...)
, and you get a null pointer exception.
因为 initialize()
是由 FXMLLoader
在它创建的实例上调用的,所以当您调用 what.setText(...)
从 initialize()
方法开始,您在 FXMLLoader
创建的实例上调用它,因此该实例的 FXML 注入字段已初始化.
Since initialize()
is invoked by the FXMLLoader
on the instance it created, when you call what.setText(...)
from the initialize()
method, you are calling it on the instance created by the FXMLLoader
, and so the FXML-injected fields for that instance have been initialized.
这篇关于JavaFX,标签空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!