J
J
java_beginner2018-02-04 13:57:40
Java
java_beginner, 2018-02-04 13:57:40

FXML & PropertyChangeListener - Cause of NPE?

Good day,
I'm trying to dock jssc and FXML app. The task is to change the value of main_label if data came from serialPort. For this I decided to use PropertyChangeListener.
Next, I used an example from the web:

public class ListenToMe  {
    
    private String variable = "Initial";
    private PropertyChangeSupport support = new PropertyChangeSupport(this);

    public void addListener(PropertyChangeListener listener) {
            support.addPropertyChangeListener(listener);
    }

    public void removeListener(PropertyChangeListener listener) {
            support.removePropertyChangeListener(listener);
    }

    public void setVariable(String newValue) {
            String oldValue = variable;
            variable = newValue;
            support.firePropertyChange("variable", oldValue, newValue);
    }
}

Added Listener to PortReader:
private static class PortReader implements SerialPortEventListener {

    @Override
    public void serialEvent(SerialPortEvent event) {
        if(event.isRXCHAR() && event.getEventValue() > 0){
            try {
                ListenToMe test = new ListenToMe();
                test.addListener(new MainScreenController());

                String buffer = serialPort.readString();
                test.setVariable(buffer);
                System.out.println("Received " + buffer);
            }
            catch (SerialPortException ex) {
                System.out.println(ex);
            }
        }
    }

MainScreenController content:
@FXML
    public Label main_label;

    @FXML
    @Override
    public void propertyChange(PropertyChangeEvent event) {
        
        String property = event.getPropertyName();
            if (property.equals("variable")) {
        
                String buffer = (String) event.getNewValue();
             
                if (buffer.equals("A")){
                    main_label.setText("Hello World!");
                }
                else if (buffer.equals("2")){
                    main_label.setText(buffer );  
                }
                else System.out.println("Got value " + event.getNewValue());  
            }
    }

As a result, as soon as "A" / "2" comes to SerialPort, I get:
Exception in thread "EventThread COM1" java.lang.NullPointerException
  at test.v0.MainScreenController.propertyChange(MainScreenController.java:42)
  at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
  at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:327)
  at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
  at test.v0.ListenToMe.setVariable(ListenToMe.java:37)
  at test.v0.MainScreen$PortReader.serialEvent(MainScreen.java:87)
  at jssc.SerialPort$EventThread.run(SerialPort.java:1112)

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question