2 Replies Latest reply on Mar 15, 2013 9:04 AM by dscarminiabielefeld

    FileChooser something is wrong with the path  Input Stream must not be null

    dscarminiabielefeld
      Hi,
      im testing the filechooser at the moment.
      this is what i got:
                      FileChooser fileChooser = new FileChooser();
                      FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.png");
                      fileChooser.getExtensionFilters().add(extFilter);
      
                      file = fileChooser.showOpenDialog(null);
      
                      iv.setImage(new javafx.scene.image.Image(getClass().getResourceAsStream(file.getPath())));
      gettin:
      java.lang.NullPointerException: Input stream must not be null
      file.getAbsolutPath() doesn't work either, i thought this was the reason.
      what am i doing wrong here?
      p.png = picture in the filechooser package.
       
      iv.setImage(new javafx.scene.image.Image(getClass().getResourceAsStream(p.png)));
      works fine.
      Thanks for the help.
      im using windows.
        • 1. Re: FileChooser something is wrong with the path  Input Stream must not be null
          jsmith
          The file chooser chooses a file from anywhere on the OS file system.
          Class resources are generally not files for JavaFX applications as javafx applications are usually packaged as jars.

          When you use getClass().getResourceAsStream(someLocation), you are specifying to use the protocol used by the class' classloader, which in a jar packaged JavaFX application will be a jar protocol, but you are not supplying a valid jar protocol location to your call, so the system cannot find the resource you are asking it to find. You cannot use a FileChooser to choose files out of a jar.

          Instead, you should use something like the following which will use the file protocol to access the file chosen from the OS filesystem by the file chooser:
          iv.setImage(new javafx.scene.image.Image(file.toURI().toURL().toExternalForm()));
          Here is a sample png image viewer application where you can see this in action: https://gist.github.com/jewelsea/5165446
          • 2. Re: FileChooser something is wrong with the path  Input Stream must not be null
            dscarminiabielefeld
            Perfect. Thank you very much. Have a nice weekend ;-)