首页 > 解决方案 > 将 JavaFX TextArea 的输入打印到控制台会出现错误

问题描述

我正在尝试将 TextArea 中用户的输入打印到控制台。

//controller
    private void btnClick(ActionEvent event) {
        System.out.println(input.getText());
    }


//fxml
...
   <left>
      <Button id="booton" fx:id="booton" mnemonicParsing="false" onAction="#btnClick" text="Button" BorderPane.alignment="CENTER" />
   </left>
   <right>
      <TextArea id="input" prefHeight="200.0" prefWidth="200.0" promptText="give me input" BorderPane.alignment="CENTER" />
   </right>
</BorderPane>

它在运行更多时给了我这个错误:

Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.TextArea.getText()" because "this.input" is null at sample.Controller.btnClick(Controller.java:31)

标签: javacssjavafxfxmlscenebuilder

解决方案


推荐阅读