私はクラスとネストされたJavaFX Controller
のコードを持っています。私はJavaFXコントローラクラスを初期化します。パブリッククラス内のネストされたJavaFXコントローラ
SpeechWindowクラス:SpeechWindow
のインスタンスを作成し、メインのJavaFXアプリケーションクラスからそれを呼び出すようにしてください
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
<center>
<TextArea fx:id="textArea" editable="false" prefHeight="200.0" prefWidth="200.0" promptText="...no response" wrapText="true" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
:
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import application.Main;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/**
* SpeechWindow
*
*/
public class SpeechWindow extends Stage {
private Container container = new Container();
/**
* Constructor
*/
public SpeechWindow() {
setTitle("SpeechResults");
setScene(new Scene(container));
}
/**
* @param text
*/
public void appendText(String text) {
container.appendText(text);
}
/**
*
*/
public void clear() {
container.clear();
}
public class Container extends BorderPane implements Initializable {
@FXML
private TextArea textArea;
public Container() {
// FXMLLOADER
FXMLLoader loader = new FXMLLoader(getClass().getResource("SpeechWindow.fxml"));
try {
loader.load();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void initialize(URL location , ResourceBundle resources) {
System.out.println("Container has been initialized..");
}
public void appendText(String text) {
Platform.runLater(() -> textArea.appendText(text + "\n"));
}
public void clear() {
Platform.runLater(textArea::clear);
}
}
}
ここでは、FXMLコードですappendText();
メソッドを呼び出します。なぜNULLポインタが出てくるのですか?
目標は次のとおりです。
はその後、私は外側のクラスのインスタンスを作成し、それを使用したいです。
行く何を間違っている:私は、メソッドappendText();
をSpeechWindow
クラスのインスタンスを作成して呼び出す場合
は、私はそれが起こるのはなぜ、私はすべてfine.Whatが行わ見る... nullポインタ例外を取得私の間違い?
Oh daaaamn、私はそれが動作するかどうかを確認し、答えとしてマークします... – GOXR3PLUS