label.setText NullPointerException [英] label.setText NullPointerException
问题描述
嗨第一次来这里,但是这里:
Hi first time here but here goes:
我有一个JavaFX应用程序,动态更改FXML UI标签,数据从Player类中提取。
I have a JavaFX application that changes the FXML UI labels dynamically and the data is pulled from a Player class.
有问题的两个类是 Player.java
和 InterfaceHandler.java
。
播放器类存储播放器详细信息,我想将详细信息传递给Interface类,该类设置标签上的文本。
The player class stores player details and I want to pass the details to the Interface class which sets the text on the labels.
作为测试,我的FXML用户界面只有一个按钮和两个标签。
As a test my FXML UI just has a button and two labels.
如果点击按钮就会调用 handleButton
方法它将 locationLabel
设置为Town罚款。
If it click the button it calls the handleButton
method it sets locationLabel
to "Town" fine.
但是如果我在我的Player类中调用 locationLabel()
方法当调用 nameLabel.setText(name)
时,我得到NullPointerException。通过调试,我发现Interface类中的名称字符串应该是Dan。
However if I call the locationLabel()
method in my Player class I get a NullPointerException when nameLabel.setText(name)
is called. Through debugging I find that the name string in the Interface class is what it should be "Dan".
任何人都可以帮忙吗?
主类:
public class Main extends Application {
public void start(final Stage mainStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
Scene scene = new Scene(root);
mainStage.setTitle("Main Screen");
mainStage.setScene(scene);
mainStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
玩家等级:
public class Player{
InterfaceHandler ui = new InterfaceHandler();
public void setNameLabel() {
String name = "Dan";
ui.setName(name);
}
InterfaceHandler类:
InterfaceHandler class:
public class InterfaceHandler implements Initializable {
public Label nameLabel;
public Label locationLabel;
public void handleButton(ActionEvent event) throws IOException {
locationLabel.setText("Town");
}
public void setName(String name){
nameLabel.setText(name);
}
}
MainScreen.fxml:
MainScreen.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" prefHeight="629.0" prefWidth="600.0" snapToPixel="true" style="-fx-background-color: beige;" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.InterfaceHandler">
<children>
<Button fx:id="button1" layoutX="512.0" layoutY="381.0" minWidth="14.0" mnemonicParsing="false" onAction="#handleButton" prefHeight="30.0" prefWidth="51.0" text="Town" visible="true" />
<Label fx:id="nameLabel" layoutX="57.0" layoutY="8.0" prefWidth="216.0" text="blank" />
<Label fx:id="locationLabel" layoutX="68.0" layoutY="27.0" prefWidth="193.0" text="blank" />
</children>
</AnchorPane>
推荐答案
这是因为你没有正确注入你的<$来自FXML文件的c $ c>标签。
It's because you didn't properly inject your Labels
from FXML file.
注释标签
vars。使用 FXML
注释:
Annotate your Label
vars. with FXML
annotation:
public class InterfaceHandler implements Initializable {
@FXML
public Label nameLabel;
@FXML
public Label locationLabel;
public void handleButton(ActionEvent event) throws IOException {
locationLabel.setText("Town");
}
public void setName(String name){
nameLabel.setText(name);
}
}
此外,InterfaceHandler是您引用的控制器< FXML中code> fx:controller 。这指示FXMLLoader在加载器加载其FXML时创建InterfaceHandler的新实例。所以不要在你的Player类中创建一个新的InterfaceHandler,而是让InterfaceHandler成为Player的构造函数参数,并使用loader.getController从FXMLLoader中获取InterfaceHandler控制器实例。
Also, InterfaceHandler is a controller which you reference with fx:controller
in your FXML. This instructs the FXMLLoader to create a new instance of the InterfaceHandler when the loader loads its FXML. So don't create a new InterfaceHandler in your Player class, instead make InterfaceHandler a constructor parameter for Player and use loader.getController to get the InterfaceHandler controller instance out of the FXMLLoader.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("MainScreen.fxml"));
Parent root = (Parent)loader.load();
Player player = new Player(loader.getController());
Scene scene = new Scene(root);
. . .
public class Player {
private InterfaceHandler ui;
public Player(InterfaceHandler ui) {
this.ui = ui;
}
public void setNameLabel() {
String name = "Dan";
ui.setName(name);
}
}
这篇关于label.setText NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!