javafx - 为什么出现 NullPointerException 然后尝试 setText?
问题描述
尝试使用 setText 方法将数据设置为TextField
. 我想要
- 向地图添加标志。
- 然后出现窗口,我将数据输入到
TextField
s。 - 在按下标志时,此窗口显示 s 中的填充数据
TextField
,但问题是我试图按下标志以获取信息,NullPointerException
显示出来。
能有问题吗?
setText
打开窗口控制器的方法:
NullPointerException
显示在setCountry
方法的第一行
public class InfoController {
@FXML
private TextField idField, nameField, countryField;
@FXML
private Button saveButton;
private String company, country;
private int code;
@FXML
void onClickClear(ActionEvent event) {
nameField.setText("");
countryField.setText("");
idField.setText("");
}
@FXML
void onSaveClick(ActionEvent event) {
if(parseInputs()) {
Information.setCode(code);
Information.setCompany(company);
Information.setCountry(country);
nameField.setText("");
countryField.setText("");
idField.setText("");
}
Stage stage = (Stage) saveButton.getScene().getWindow();
stage.close();
}
private boolean parseInputs() {
try {
company = nameField.getText();
country = countryField.getText();
code = Integer.parseInt(idField.getText());
if(nameField.getText().isEmpty() || countryField.getText().isEmpty() || idField.getText().isEmpty()) return false;
}
catch (Exception e) {
System.out.println("Unable to save information");
return false;
}
return true;
}
public void setCountry(String data) {
countryField.setText(data);
countryField.setEditable(false);
}
位置,在打开窗口之前尝试设置文本的位置。
@FXML
void addFlag(ActionEvent event) {
addingFlag.pinFlag(imageView, anchPane);
flagList = addingFlag.listOfFlags();
Button flg = flagList.get(0);
flg.setOnMouseClicked(eve -> {
String getCode = Integer.toString(Information.getCode());
info.setCountry(Information.getCountry());
info.setCode(getCode);
info.setCompany(Information.getCompany());
addingFlag.openInformationWindow();
});
}
解决方案
推荐阅读
- google-play - 如何在同一屋檐下管理 SSO、Chrome 扩展程序和 Google Play 商店帐户
- java - 后增量,前增量。JAVA
- java - 太多的守护线程导致程序失败
- javascript - 设置全局正文字体大小不起作用
- react-native - 如何在 ReactNative 中使用 DateTimepicker 在文本中显示日期?
- java - 在对另一个 API 进行一定数量的休息后调用后,我的 Spring Boot 应用程序收到 400 个错误请求和空数据
- google-bigquery - 获取在一个特定事件之前执行的所有事件?
- javascript - 如何验证 URL 以确认 Angular 6 中的视频链接?
- javascript - 如何从 XML 中的记录中检索数据
- vmware - 在 vmware 上安装 Zeppelin 和 spark