0

I am having some issues getting my programs to run. Basically step 1 of my program is to use the JFileChooser to open up an image and make it into a buffered image, simple enough right? this is what I have:

JButton open = new JButton();
            JFileChooser fc = new JFileChooser();
            File selectedFile = fc.getSelectedFile();
            fc.setDialogTitle("Please choose an image...");
            FileNameExtensionFilter filter = new FileNameExtensionFilter("JPEG", "jpeg", "jpg", "png", "bmp", "gif");
            BufferedImage origImage = null;

            String path = "";
            File f = fc.getSelectedFile();
            boolean exists = false;
            fc.addChoosableFileFilter(filter);


            try {

                f = fc.getSelectedFile();
                exists = f.exists();
                path = f.getAbsolutePath();

                origImage = ImageIO.read(new File(path));
            }
            catch(Exception e) {
                System.out.println(e);
                System.exit(0);
            }

im getting a null pointer exception (caught by my catch statement) I think it has something to do with the getbsolutepath, but im not sure. Any ideas? Thanks!

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Elchapo
  • 69
  • 1
  • 3
  • 13

2 Answers2

3

You never seem to actually open the file chooser, so no file is ever selected which would account for the NullPointerException

JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Please choose an image...");
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPEG", "jpeg", "jpg", "png", "bmp", "gif");
fc.addChoosableFileFilter(filter);

BufferedImage origImage = null;
// You should use the parent component instead of null
// but it was impossible to tell from the code snippet what that was.
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
    File selectedFile = fc.getSelectedFile();
    try {
        origImage = ImageIO.read(selectedFile);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

Take a look at How to Use File Choosers for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • how would I go about making it terminate the program if the user hits cancel on the JFileChooser pane? – Elchapo Apr 14 '16 at 05:55
  • @Elchapo There's lots of ways you might be able to do it depending on your code, you could use a `else` statement to simple call `System.exit(0)`, but I always find that a bit harsh. It might be better to check to see if `origImage` is `null` or not, if it is, you just let the execution fall through to the end of the method and if it's not `null`, you process it in what ever way you want – MadProgrammer Apr 14 '16 at 05:59
  • Thanks a bunch! the origImage == null makes sense! Thanks!! – Elchapo Apr 14 '16 at 06:01
  • 1
    And `new FileNameExtensionFilter("JPEG", "jpeg", "jpg", "png", "bmp", "gif");` would best be more like: `new FileNameExtensionFilter( "Image files", ImageIO.getReaderFileSuffixes());` See [this answer](http://stackoverflow.com/a/13521592/418556) for more details. – Andrew Thompson Apr 14 '16 at 08:40
-2

Try this

path = f.getAbsolutePath().replace("\", "\\");

AssenKhan
  • 576
  • 5
  • 15
  • 1
    It's not required, `ImageIO.read` can take a `File` reference, so the OP shoudln't be messing about with `String` file names anyway and since the OP never actual states them, there should be no reason to change then as the API will have already accounted for them – MadProgrammer Apr 14 '16 at 05:39