【发布时间】:2017-08-15 15:40:36
【问题描述】:
我有一个用于将系统消息发布到 GUI 的接口,但我这样做的方式会导致尝试使用接口方法的类(而不是实现它的方法)出现 NULL POINTER EXCEPTION
public interface SystemMessage {
void postMessage(String outText);
}
在我的控制器中,我实现了这个接口并使用它向 GUI 发布消息
public class MainController implements SystemMessage {
@FXML
public DialogPane systemMessage;
@Override
public void postMessage(String outText) {
systemMessage.setContentText(outText);
}
}
我从 Main.java 调用一些辅助类来做一些后台工作(我还没有线程化所以借口一切都在主线程上)
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("fxml/entry.fxml"));
Parent root = loader.load();
this.mainController = loader.getController();
primaryStage.setTitle("Title");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
initializeSync();
}
private void initializeSync() {
//Each of these perform several function on initialization
Identity identity = new Identity();
String[] args = null;
Api api = new Api();
api.Get(args);
SqLite db = new SqLite();
}
所以,在我的 Identity Class 初始化时,我尝试使用接口发布消息,但我得到 NULL POINTER EXCEPTION。
public class Identity {
private String machineId = null;
public String statusText = null;
SystemMessage systemMessage;// Trying to instantiate the system message interface??
public Identity(){
systemMessage.postMessage("Checking Machine Identity");
//..//
}
//..//
}
【问题讨论】:
标签: java class javafx interface