我正在尝试操纵Scene Builder生成的TextField中的文本。我的控制器如下所示:

@FXML
private TextField textDescr;

public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
    textDescr = new TextField();
    assert textDescr != null : "fx:id=\"textDescr\" was not injected: check your FXML   file 'provingGroundsUI.fxml'.";
    Game.mainFSM.enter();
}
public void setText(String s) {
    // TODO Auto-generated method stub
    textDescr.setText(s);
}


我收到了NullPointerException。我尝试过带和不带textDescr = new TextField();部分的机器人。我不太了解...。我以为JavaFX在程序开始时初始化了所有UI变量。

最佳答案

您的FXML外观如何?
在setText函数中操作textDescr具有很多风险。最好使用绑定的StringProperty:




    @FXML
    private Text textDescr;

    private StringProperty textProperty = new SimpleStringProperty();

    @FXML
    void initialize() {
            assert textDescr != null : "fx:id=\"textDescr\" was not injected: check your FXML file 'TestView.fxml'.";
           textDescr.textProperty().bind(textProperty);
    }

    public ReadOnlyStringProperty textProperty(){
          return textProperty;
    }

07-24 00:21