Я пытаюсь получить объект пути следующим образом:
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);
}
}
Однако я получаю исключение NullPointerException, когда пытаюсь вывести список строк другим методом в классе EditorController следующим образом:
@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