0

I'm making a simple graphical desktop application for image management and, I'm using JavaFX, in addition to its sceneBuilder. The idea is, at pushing a button a FileChooser appears, to choose the image and to show a new window with all the background image on it. The problem is that in doing it. I get an error that I can not identify.
Edit: I discovered that if I open the image in the same window there is no error.
My code:

public void OpenWindow(ActionEvent event) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("Window2.fxml"));
        Scene secondScene = new Scene(root,800,800);
        Stage newWindow = new Stage();
        newWindow.setTitle("Imagen");
        newWindow.setScene(secondScene);
        newWindow.setX(100);
        newWindow.setY(100);
        newWindow.show();
        /////// Open Window //////
        FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter(" JPG", "*.JPG");
        fileChooser.getExtensionFilters().addAll(extFilterJPG);
        File file = fileChooser.showOpenDialog(null);

        try {
            BufferedImage bufferedImage = ImageIO.read(file);
            Image image = SwingFXUtils.toFXImage(bufferedImage, null);
            myImageView.setImage(image);

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


    }

Edit: (SOLUTION WAS CREATE A NEW FMXL LOADER) `

    FXMLLoader loader = new FXMLLoader(getClass().getResource("SecondController.fxml"));
    Parent root = (Parent) loader.load();
    SecondController secController = loader.getController();
    secController.nuevaImagen(imagen);
    secController.mostrarInfo(imagen); // Hacer que el controlador de la imagen muestre la info
    secController.addMainController(this);
    Stage stage = new Stage();
    stage.setScene(new Scene(root));
    stage.setTitle(datosImagenActiva.titulo);
    stage.show();`
Danielmagox
  • 112
  • 7
  • Is that the full stack trace? Are there any `Caused by:`s you left out? – Slaw Nov 17 '18 at 18:44
  • Yes, there are 2: `Caused by: java.lang.reflect.InvocationTargetException` & `Caused by: java.lang.NullPointerException` – Danielmagox Nov 17 '18 at 18:45
  • That means the fundamental cause is a `NullPointerException`. Read [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) and [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it), then go to where the stack trace is telling you the error is so you can see the problem. – Slaw Nov 17 '18 at 19:01
  • I guess it's `myImageView`. OP might have forgotten to put `fx:id` in the FMXL – Gnas Nov 17 '18 at 19:04
  • Nope it is too. – Danielmagox Nov 17 '18 at 19:12
  • @Danielmagox Did you follow the links provided Slaw? Once you know which line throws the NPE it becomes a lot easier to solve. – Gnas Nov 17 '18 at 20:37
  • @Gnas yes i did the line is `myImageView.setImage(image);` but i dont know why, and the only thing I found is that if I open it in the main window it works without problems. – Danielmagox Nov 17 '18 at 21:14
  • By opening in the main window do you mean passing the current window to `fileChooser.showOpenDialog`? – Gnas Nov 17 '18 at 22:57
  • @Gnas No, I mean that the image instead of opening it in a new window I open it in the window that is created by default – Danielmagox Nov 17 '18 at 23:39
  • I still don't understand what you mean, can you provide the code where you open it in the main window which works? – Gnas Nov 17 '18 at 23:45
  • I deleted the code until //////open Window////// and in Main class i have: `public class Main extends Application { @Override public void start(Stage primaryStage) { try { Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); Scene scene = new Scene(root); primaryStage.setTitle("Window"); primaryStage.setScene(scene); primaryStage.show(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } }` – Danielmagox Nov 17 '18 at 23:57

1 Answers1

0

You need to pass a Stage object to fileChooser.showOpenDialog. Try this:

File file = fileChooser.showOpenDialog(newWindow);

Edit: This is false as pointed out by @Slaw

Gnas
  • 698
  • 1
  • 6
  • 14
  • The [documentation](https://openjfx.io/javadoc/11/javafx.graphics/javafx/stage/FileChooser.html#showOpenDialog(javafx.stage.Window)) indicates the argument may be `null`. – Slaw Nov 17 '18 at 18:57
  • I think you misundertstood. It doesn't say the argument can be null, only the result if no file is chosen – Gnas Nov 17 '18 at 18:59
  • "_**If** the owner window for the file dialog is set..._" – Slaw Nov 17 '18 at 19:00
  • I just tried it and it gives the same error. – Danielmagox Nov 17 '18 at 19:00
  • @Slaw you're right, I just tried setting it to null in my existing code and it works. – Gnas Nov 17 '18 at 19:03