我正在使用我的java fx应用程序从串行端口写入/读取。一切正常,直到我从端口读取数据。收到数据后,将其传递给位于控制器中的方法进行处理。将数据传递到控制器中的方法后,我试图将其分配到我的文本框中,但是会引发NPE异常。我什至尝试使用Platform.runLater(),但仍然给我同样的NPE消息。有什么帮助吗?
//Serial data reader
@Override
public void serialEvent(SerialPortEvent event) {
if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_RECEIVED)
return;
packet = event.getReceivedData();
// Also tried this myController.processData(packet); Still NPE
Platform.runLater(()->
{
settingsController.processData(packet);
});
}
//The method in myController
@FXML
private JFXTextField dataTextField;
public void processData(int[] packet) {
dataTextField.textProperty().setValue(String.valueOf(packet[2])); //Here it throws NPE due to dataTextField
//dataTextField.textProperty().setValue(String.valueOf(5)); Also tried a dummy value to find out if it is text field or the packet data. I confirmed it is NPE of dataTextField
//I also tried this .Still the NPE. Also tried with combobox. so no hope
Platform.runLater(()->
{
dataTextField.textProperty().setValue(String.valueOf(packet[2]));
});
}
答案 0 :(得分:0)
您尝试过使用
dataTextField.setText(String.valueOf(packet[2]));
还要检查是否为fxml文件中的文本字段分配了相同的fx:id=dataTextField
。
发布完整的堆栈跟踪