user3152750
user3152750

Reputation: 128

How to fetch a Path Object from FileChooser JavaFX

I'm trying to fetch a Path Object like this:

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);
    }
}

However, I get a NullPointerException when I try to output the String List in another method in the EditorController class like this:

@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();
}

This is exactly what I get: http://pastebin.com/QtzQ9RVZ

The EditorController.java:40 is this code: this.textareaContent.appendText(line + "\n");

The TextEditorController.java:38 is this code: editorController.openEditor(lines);

How do I fetch it correctly and then show it on my TextArea? Note that I want to use java.nio and not java.io

Upvotes: 1

Views: 477

Answers (1)

James_D
James_D

Reputation: 209340

It has nothing to do with the way you are getting the file, the issue is you have two different instances of EditorController. When you execute FXMLLoader.load(...), the FXMLLoader creates an instance of your controller class for you and populates the @FXML-annotated fields. So that instance has textAreaContent initialized, but the instance you create with new EditorController() (and on which you are calling openEditor) does not.

Refactor it like this:

EditorController:

@FXML
public TextArea textareaContent;

@FXML
private Parent root;

public void openEditor(List<String> lines) throws IOException {
    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();
}

And add an fx:id="root" attribute to the root element of Editor.fxml.

Then do

@FXML
public void handleOpenFileAction(ActionEvent event) {       
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open a File");
    this.file = Paths.get(fileChooser.showOpenDialog(new Stage()).toURI());
    // note the slightly cleaner version:
    // this.file = fileChooser.showOpenDialog(new Stage()).toPath();


    try {
        List<String> lines = Files.readAllLines(this.file, Charset.defaultCharset());

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/HassanAlthaf/Editor.fxml"));
        Parent root = loader.load();
        EditorController editorController = loader.getController();
        editorController.openEditor(lines);

    } catch(IOException ex) {
        System.out.println(ex);
    }
}

Upvotes: 1

Related Questions