簡體   English   中英

如何在另一個類JavaFX的textarea上設置文本

[英]How to set text on textarea in another class JavaFX

我在另一個課程的textarea上設置文本遇到了問題。 完成簡單的腳本后,我想使用InputStreamReader在另一個類中更改textarea。 我有的錯誤是:java.lang.NullPointerException。 我已經嘗試過使用get()和set()方法,如果我在包含InputStreamReader的類中使用具有初始化值的字符串,並嘗試在具有textarea的類中進行sysout,它將向我顯示該字符串,但是如果我要setText則有一個NPE 。 SimpleStringProperty是好的解決方案嗎?

輸出為:

            Process process;
            process = Runtime.getRuntime().exec(PATH_TO_SIKULI + " -r " + s.toString());
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String as=null;
            stringBuilder = new StringBuilder(as);

            while ((as = stdInput.readLine()) != null) {
                stringBuilder=stringBuilder.append(as+"\n");
            }
            while ((as = stdError.readLine()) != null) {
                stringBuilder=stringBuilder.append(as+"\n");
            }
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();

            }
    }

具有TextArea的類

公共類ResultController實現Initializable {

@FXML
public TextArea resultTextArea;

@Override
public void initialize(URL location, ResourceBundle resources) {
    Platform.runLater(new Runnable() {
        @Override
        public void run() {

        }
    });



}

這會引發您的NullPointer:

String as=null; 
StringBuilder stringBuilder = new StringBuilder(as);

作為文檔的很好的描述,您不能將null傳遞給StringBuilder構造函數。 情況就是這樣: as = null

java.lang.StringBuilder.StringBuilder(String str)

構造一個字符串構建器,初始化為指定字符串的內容。 字符串生成器的初始容量為16加上字符串參數的長度。

參數:str緩沖區的初始內容。 拋出:NullPointerException-如果str為null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM