1

When I am giving absolute path of image means from my project scr directory it loads the image but when I am trying to load image from specified path or relative path then it shows null error, it doesn't read image.
i have get path using JFilechooser directory specifically, it only display .jpg image and give list of files to File type and stored one by one image in string variable and then want to load image using that variable.

My code:

final String fpath;
final File dir = new File("");
final JFileChooser file;
file = new JFileChooser();
file.setCurrentDirectory(dir);
file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
file.showOpenDialog(contentPane);
final File[] lofFile = file.getCurrentDirectory().listFiles();

for (int i = 0; i < a; i++) {
    if (lofFile[i].toString().substring(lofFile[i].toString().lastIndexOf(".") + 1).equals("jpg")) {
        az = lofFile[i].toString();
    }
}

private BufferedImage bg;
bg = ImageIO.read(getClass().getResource(az));

Throws this exception:

java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source)

Any help please?

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Java Curious ღ
  • 3,622
  • 8
  • 39
  • 63
  • The JVM **never** prints "shows error." What is the _actual_ error message? – Sean Bright Aug 12 '13 at 12:52
  • post code where you enter on "Open" dialog button – Maxim Shoustin Aug 12 '13 at 12:54
  • @Sean Bright - java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source) is an error, sorry i have just write that to highlight. – Java Curious ღ Aug 12 '13 at 12:54
  • If you knew the names of the files inside a given directory, then you can simply use this [approach](http://stackoverflow.com/a/15187181/1057230). Hopefully this might help :-) Though I doubt, `getClass().getResource()` will work in this situation, since this is used to get the location relative to the `.class/package`. So what if your `.class` file is in `Drive E` and you are trying to access images from `Drive F`, you need to device a way to convert the relative path in such a way, that it can move to the image file, relative to the location of `.class` file, IMHO. – nIcE cOw Aug 12 '13 at 12:54
  • @nIcEcOw - but i exactly dont know all files, so that's why i have written up this long code to fetch up file from choosen directory. – Java Curious ღ Aug 12 '13 at 13:03
  • @user2659972 : Please have a look at [DirectoryStream](http://docs.oracle.com/javase/7/docs/api/java/nio/file/DirectoryStream.html), there is small code snippet, which can help you know the names of each files. – nIcE cOw Aug 12 '13 at 13:06
  • @nIcEcOw - i have solved that using below solution. by the way thank u so much because you have told me to directly pass File Type as an argument. – Java Curious ღ Aug 12 '13 at 13:16
  • @user2659972 : Please have a look at this code [example](https://www.dropbox.com/s/4ncqkcj6hyvm78u/DirectoryFiles.java), here you specify the folder which contains the images, and it loads all images from the folder and display them on the `JPanel`. I hope you can modify this according to your needs :-) – nIcE cOw Aug 12 '13 at 13:33
  • @ niCe cOw - sir if there is any simple example of it then please help me, i have tried ArrayList = lofFile but it doesn't work. also tried List but not work. – Java Curious ღ Aug 12 '13 at 13:37
  • @user2659972 : Please find the [simplest example](https://www.dropbox.com/s/75h5u9effv6ph6r/LoadImageFromDirectory.java), though do change the directory path, according to your `FileSystem` (mean to say, as to where your images are on the hard disk). I hope this will help :-) – nIcE cOw Aug 12 '13 at 14:02

1 Answers1

2

Class#getResource is not for loading files from the file system. Maybe ImageIO.read(az) will work.

But you get a File object from the JFileChooser. Use it and you have no problem at all. Don't do any String manipulation on filenames if there is no reason for it. Filtering for a specific suffix is not a reason.

Hauke Ingmar Schmidt
  • 11,559
  • 1
  • 42
  • 50
  • no its not working it need File type argument. it only read File Type argument data. – Java Curious ღ Aug 12 '13 at 13:02
  • yes that's right, after using directly File Type variable as argument with ImageIO.read then it works smoothly and easily, thank you so much. – Java Curious ღ Aug 12 '13 at 13:15
  • @ nIcE cOw - sir i want add one more facility in this code is that if user click button then next image should be loaded in image object, if you just give me guidance then i am so thankful to you. – Java Curious ღ Aug 12 '13 at 13:19