【发布时间】:2015-07-02 11:58:13
【问题描述】:
我正在尝试获取这样的路径对象:
private Path file;
private String fileContent;
private Parent root;
@FXML
public void handleOpenFileAction(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open a File");
this.file = Paths.get(fileChooser.showOpenDialog(new Stage()).toURI());
try {
List<String> lines = Files.readAllLines(this.file, Charset.defaultCharset());
EditorController editorController = new EditorController();
editorController.openEditor(lines);
} catch(IOException ex) {
System.out.println(ex);
}
}
但是,当我尝试在 EditorController 类中的另一个方法中输出字符串列表时,我得到了 NullPointerException,如下所示:
@FXML
public TextArea textareaContent;
public Parent root;
public void openEditor(List<String> lines) throws IOException {
this.root = FXMLLoader.load(getClass().getResource("/com/HassanAlthaf/Editor.fxml"));
Scene scene = new Scene(this.root);
Stage stage = new Stage();
stage.setScene(scene);
stage.setTitle("Editting File");
for(String line : lines) {
this.textareaContent.appendText(line + "\n");
}
stage.show();
}
这正是我得到的:http://pastebin.com/QtzQ9RVZ
EditorController.java:40 是这个代码:this.textareaContent.appendText(line + "\n");
TextEditorController.java:38 是这个代码:editorController.openEditor(lines);
如何正确获取它,然后在我的 TextArea 上显示它?请注意,我想使用java.nio 而不是java.io
【问题讨论】:
标签: java javafx filechooser