0

I have that java class. It's initialized in JavaClassContainer. The problem is that it returns that error: java.lang.NullPointerException. A no set the text to the textfield.

import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import java.io.BufferedReader;
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.util.Enumeration;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;

public class RXTX implements SerialPortEventListener{

    @FXML private static TextField carlos;

   // MORE CODE
@FXML
    private void GetData(String Data) {
            if(Data.contains("Temperature")){
                carlos.setText("Hola");
            }
        }
}

If I put a System.Out.Println for instance it works. So, The problem is in the setText to the TextField no in GetData class.

Ok, I will put also the main controller:

public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        RXTX main = new RXTX();
        main.initialize();
        Thread t=new Thread() {
            public void run() {
                //the following line will keep this app alive for 1000 seconds,
                //waiting for events to occur and responding to them (printing incoming messages to console).
                try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
            }
        };
        t.start();
        System.out.println("Started");
    }    

}
user3203690
  • 113
  • 1
  • 1
  • 10
  • Can you add full exception stack trace? It gives you line number which helps to understand what is null in NullPointerException. – Sergey Grinev Mar 22 '14 at 21:45
  • How Can i do that in a if? – user3203690 Mar 22 '14 at 21:48
  • You said you have `java.lang.NullPointerException`. Usually it's a part of stack trace, like here: http://stackoverflow.com/questions/10464547/how-to-solve-java-lang-nullpointerexception-error – Sergey Grinev Mar 22 '14 at 21:50
  • No, It just give that. Nothing more! I think is a problem when you run a setText in a clase. I found other posts like this but I didn't find the solution. – user3203690 Mar 22 '14 at 21:56
  • add test output in your buttonaction: `System.out.println("Data=" + Data + " carlos=" + carlos);` to see what is null. Also, why `carlos` is static? – Sergey Grinev Mar 22 '14 at 23:35
  • Can you post the code where 'carlos' is initialized? Is it in the initialize() method you call from the controller's initialize() method? – James_D Mar 23 '14 at 00:42

1 Answers1

0

Well - what makes you believe that your RXTX.carlos is injected a value? You are creating the instance yourself so you are responsible to set a value to the STATIC field there.

I think you should:

  1. learn about how to use a debugger in your IDE
  2. learn more about how FXML works
tomsontom
  • 5,856
  • 2
  • 23
  • 21