r/javahelp • • May 08 '22

Why am I getting javafx.fxml.LoadException JavaFX?

package application;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Scanner;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;


public class Controller implements Initializable{

    @FXML
    private TextArea document;

    @FXML
    private TextField findText;

    @FXML
    private TextField replaceText;

    @FXML
    private Button run;
    
    @FXML
    private Button browse;
    
    
    String selectedFilePath;

    @FXML
    public void replace(ActionEvent e){

    }

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		try {
			Scanner sc = new Scanner(new File(selectedFilePath));
			
			while(sc.hasNextLine()) {
				document.appendText(sc.nextLine() + "\n"); 
			}
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} 
		
	}

	@FXML
	public void browse(ActionEvent e){
		FileChooser fc = new FileChooser(); 
		File selectedFile = fc.showOpenDialog(null); 
		
		fc.getExtensionFilters().addAll(
				new ExtensionFilter("Text Files", "*.txt") );
		
		selectedFilePath= selectedFile.getPath();
		System.out.println(selectedFilePath);
		
	}
	
}

The error points out the mistake in the initialize method, for the Scanner object. Why is that so? How can I fix it?

Edit: For additional Reference, I am making Java GUI where a button lets you open a file and print it's content in the text area

3 Upvotes

Duplicates