mneri
mneri

Reputation: 2217

getResources() returns null

Using Eclipse IDE. The line:

getClass().getResource("/res/bitmaps/image.png");

returns null. I have created the res folder in the root of my project.

The code of interest is:

bImage = ImageIO.read(getClass().getResource("/res/bitmaps/image.png"));

and it throws the exception:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1378)
    at com.example.game.resource.Resources._loadImage(Resources.java:31)
    at com.example.game.GameComponent.<init>(GameComponent.java:19)
    at com.example.game.GameFrame.<init>(GameFrame.java:8)
    at com.example.game.GameFrame.main(GameFrame.java:13)

Any help?

Upvotes: 2

Views: 6812

Answers (4)

Pau Kiat Wee
Pau Kiat Wee

Reputation: 9505

ImageIO.read(getClass().getResourceAsStream("res/drawable/image.png"));

Make sure res folder is in class path, verify using project properties > Java build Path > Source tab. If not in class path, can add via Add Folder.. button on the right.

Upvotes: 4

Java42
Java42

Reputation: 7706

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: package_name/name

Where the package_name is the package name of this object with '/' substituted for '.' ('\u002e').

Upvotes: 0

Chandra Sekhar
Chandra Sekhar

Reputation: 19502

getClass().getResource("/res/drawable/image.png");

You should give the path of the folder in which image.png exists

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500525

You say the resource is in "the root of my project" - is it that folder in your build path? You need to have it in your build path so that Eclipse will copy it to the output directory (bin by default).

Upvotes: 1

Related Questions