简体   繁体   English

使用FXML将setText转换为TextFiend JavaFX

[英]setText into TextFiend JavaFX with FXML

I have that java class. 我有那个Java类。 It's initialized in JavaClassContainer . 它在JavaClassContainer初始化。 The problem is that it returns that error: java.lang.NullPointerException . 问题是它返回该错误: java.lang.NullPointerException A no set the text to the textfield . 否将文本设置为文本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. 如果我放一个System.Out.Println例如,它可以工作。 So, The problem is in the setText to the TextField no in GetData class. 因此,问题出在GetData类中的setTextTextField中。

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");
    }    

}

Well - what makes you believe that your RXTX.carlos is injected a value? 那么-是什么让您相信RXTX.carlos被注入了价值? You are creating the instance yourself so you are responsible to set a value to the STATIC field there. 您是自己创建实例的,因此您有责任在此处为STATIC字段设置一个值。

I think you should: 我想你应该:

  1. learn about how to use a debugger in your IDE 了解如何在IDE中使用调试器
  2. learn more about how FXML works 了解有关FXML如何工作的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM