-1

I am writing/reading from serial port with my java fx application. All is fine until I read the data from port. Once I received the data I pass it to the method thats located in my controller for processing. After passing the data to the method in controller I am trying to assign it into my textboxes but it throws NPE exception. I even tried with Platform.runLater() but still gives me the same NPE message. Any help please?

//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])); 

            }); 
}
Matahari
  • 109
  • 1
  • 3
  • 12
  • Please provide a [mcve] that demonstrates the problem. – kleopatra Aug 06 '18 at 07:44
  • Somebody marked this as duplicate. It is not the same thing. In the public void processData(int[] packet){ dataTextField.setText("test") } // this works fine but it does not work with the data that returns from port read. – Matahari Aug 06 '18 at 13:24
  • something wrong in the code you are not showing as I already suggested a couple of hours ago ;) – kleopatra Aug 06 '18 at 13:31

1 Answers1

0

Have you tried using

dataTextField.setText(String.valueOf(packet[2]));

Also check if the text field in the fxml file is assigned the same fx:id=dataTextField.

Post the full stack trace

Black
  • 81
  • 1
  • 14