【发布时间】:2016-10-30 15:46:33
【问题描述】:
我有下面的代码,其中包含一个类和一个嵌套的JavaFX Controller。我初始化了 JavaFX 控制器类,我得到了它的一个实例,就像你看到的那样。
SpeechWindow 类:
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代码:
<?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>
尝试从 Main JavaFX Application 类中调用它,创建SpeechWindow 的实例并调用appendText(); 方法。为什么会出现空指针?
目标是:
然后我想在外部类中创建一个实例并使用它。
出了什么问题:
如果我创建 SpeechWindow 类的实例并调用方法 appendText(); 我得到空指针异常...为什么会发生这种情况,我看到一切都很好。我的错误是什么?
【问题讨论】: