0

I'm using a simple way to get my resources for the project. I'm using Eclipse, and I have a 'res' folder to hold the needed files. This is how I load stuff, for example a 'puppy.png' just in my res folder (no subfolders):

String path = "/puppy.png";
try {
    BufferedImage image = ImageIO.read(getClass().getResourceAsStream(path));
} catch(Exception ex) { ex.printStackTrace(); }

And sometimes I get an input==null error, and sometiomes not! Not like this time puppy.png loaded but next time it won't. For some classes it always loads correctly, and for the other classes I always get this error. Can anyone explain why can this happen, and how can I fix it, but still use the getResourceAsStream() method?

5
  • Try using a backslash instead of a forward slash. It may or may not help. EDIT: You may not even need the slash at all Commented Apr 22, 2014 at 19:32
  • 1
    A backslash is not the answer. Commented Apr 22, 2014 at 19:34
  • That's why I provided it as a comment rather than an answer. And see my edit, I realized the possibility that I was wrong anyway Commented Apr 22, 2014 at 19:35
  • It doesn't, I've tried. Commented Apr 22, 2014 at 19:35
  • Maybe you should also print out the classloader of the class... Commented Apr 22, 2014 at 19:40

2 Answers 2

1

Please have a look at How to retrieve image from project folder?.

I have mentioned no of ways to read image from different paths.

You can try any one

// Read from same package 
ImageIO.read(getClass().getResourceAsStream("c.png"));

// Read from absolute path
ImageIO.read(new File("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\c.png"));

// Read from images folder parallel to src in your project
ImageIO.read(new File("images\\c.jpg"));

In your case the image must be in the same package where is the class and don't prefix /.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes also see the javadoc of the method: Before delegation, an absolute resource name is constructed from the given resource name using this algorithm: If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'. Otherwise, the absolute name is of the following form: modified_package_name/name Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').
0

Note that if the resource returns null (meaning it doesn't exist), you will get this error.

Check the input returned like so:

String path = "/puppy.png";
try {
    InputStream is = getClass().getResourceAsStream(path);
    if (is == null) {
        //resource doesn't exist
    } else {
        BufferedImage image = ImageIO.read(is);
    }
} catch(Exception ex) { ex.printStackTrace(); }

Note that you most likely should be using String path = "puppy.png", seeing as you will already be in the content of the project folder.

7 Comments

How could it be? it does exist!
If it does not find it, then it will return in that form. Try checking the value returned from getClass().getResourceAsStream(path)
Careful. getResource does not expect a file system specific separator. It expects / literally.
Aha, in that case I'll change my answer then.
The main problem is that the resource exists but sometimes cannot be found. This would treat it safely but doesn't solve the problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.