尝试绑定字符串属性的空指针

Null Pointer trying to bind a stringproperty

我尝试 绑定 一个 String 和一个 StringPoperty,我认为这可行 :

@FXML private TextArea consoleTextArea;
StringProperty sp = new SimpleStringProperty();
consoleTextArea.textProperty().bind(sp);

但它 return 我一个 NULL Pointer Exception,为什么?

EDIT :经过一些回答后,我的 consoleTextArea 似乎是 NULL,但我正确地初始化了它: <TextArea id="consoleTextArea" fx:id="consoleTextArea" prefHeight="309.0" prefWidth="600.0" VBox.vgrow="ALWAYS" /> 我在调用控制器 之前创建了 window,所以我不明白为什么 NULL ...

  • 你的consoleTextArea初始化了吗?这是由 FXML 加载器完成的,因此请确保在尝试使用此字段之前先调用它。
  • 您是否在 Scene Builder 中将 TextArea 的 fx:id 设置为 consoleTextArea
  • TextArea 是否包含任何文本?
  • 尝试用 new SimpleStringProperty("") 初始化 sp。

其中一个应该修复它。