Problems with loading images

To load my images I have decided ot use the class BufferedImage. To make things a little easier I have created a method which looks like this:

private BufferedImage loadImage(String path)
	{
		try
		{
		    img = ImageIO.read(new File(path));
		    return img;
		}catch (IOException e){}
		return null;
	}

Using this code does however output me an error (@ line: 15). The line on which this error (NullPointerException) appears looks like this:

g.drawImage(game.loadImage("/textures/dot.png"), 0, 0, null);

I am still learning this all, but I am pretty sure that the problem is related to my project settings since I have tried to load images on a bunch of different ways but the result was always the same.

This is my .classpath file:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="src"/>
	<classpathentry kind="src" path="textures"/>
	<classpathentry kind="con" path=". . ."/>
	<classpathentry kind="output" path="bin"/>
</classpath>

I have created the folder “textures” with the help of Eclipse and set it as a project folder.

And yes, this folder does contain the file “dot.png”.

Thanks in advance!

File takes an absolute path. It is better to do this:


ImageIO.read(getClass().getResource("/textures/dot.png"));

Note that using a leading forward slash indicates the working directory.

Also, it is not smart to read an image every time you draw it. That will cause a lot of slow down plus it is a waste of memory. Store the BufferedImage in an instance variable once.

This is how i’m rendering Images that don’t need any transparency.


	public static Image Sprite;
	public static ImageIcon player_2 = new ImageIcon("sprites/player/player_2.png");

	public static void renderPlayer(Graphics g) {
		final Graphics2D g2d = (Graphics2D) g;
      //Define that the Image: 'Sprite' is now = our players Image
		Sprite = player_2.getImage();
      //Than draw the Sprite defined above.
		g2d.drawImage(Sprite, PlayerEntity.absX, PlayerEntity.absY, null);
	}

Hope it helps some.

Please do not use ImageIcon. Use ImageIO.read, as the OP correctly did.

Never ever used these lines, even once.

getClass().getResource

just saying =P

Same for me, I find it difficult, or over 50% of the time it just returns null/cannot find.

getClass().getResource(String) and getResourceAsStream(String) use the class’s location to look for resources. Appending a forward slash at the beginning makes it look at the root of the application package hierarchy.

For example, lets say the class MyClass is under com.mygame.mypackage. If the file you want is “image.png”, you put it under com/mygame/mypackage/ and do MyClass.class.getResource(“image.png”) or MyClass.class.getResource("/com/mygame/mypackage/image.png").

This mostly depends on the classloader used but according to experience, this works with Jar files, applets, and webstart.

oww typing on a touch screen phone is painful ;D

yeah but who would do that, putting ressources/assets in the same folder source files/bytecode is

Yes, basically, only useful in certain situations, like having, what they call a “fat jar”, meaning a jar which actually also contains the ressources.
In that case its the only (?) way to access the stuff.

Alright then if you have a dedicated resources folder, let’s call it “res”, you put it under your “src” folder in Eclipse and then you do getResource("/res/" + myFileName);

I was overwhelmed with the multitude of image loading methods when I started… ImageIO.read(this.getClass().getResource("/images/sprite.PNG")); solved all my problems haven’t looked back since

I always put res folder along with bin and src :wink:

It’s best to put “res” under “src” because it makes exporting to JAR file easier :slight_smile:

But I have experience where eclipse failed in exporting, so I have open the jar with 7z and add the res. Forget how it came exactly.

You have to set the settings properly. They’re quite confusing at first. Also, you should make a “jardesc” file (2nd screen in the exporting process). This will save the exporting settings in a filename.jardesc file and allow you to automate all future exportings.

It works with loose files as well, not just those situations above. For example, if I have test.png in the same folder as a class file (in the default package in this particular example), I can do this to display it:


panel.add(new JLabel(new ImageIcon(getClass().getResource("/test.png"))));

res is not source. it has no business in the src folder

src holds .java files only
bin only class files
everything else is accessed from the root.

example:
root has src, bin, cfg, music, sounds, img, maps, bla

alternatively you can group stuff

we use a “content” folder for everything fix and maps are seperate and so are configs and logs

when dealing with a big game you should really structure your stuff

Resources are source :wink:

Behold the One True Layout:


src/
    main/
        java/
        resources/
    test/
        java/
        resources/

I am ra4king and I wholeheartedly agree with the above post (except I would rename resources to res because it makes me feel better ;D)

:o How darest thou besmirch the One True Layout?! >:(

Seriously, that’s the maven defaults (and sbt, and buildr, and gradle). It’s not hard to change it in the config, but the defaults mean not having to write any of that config. What’s really handy though is the separate directory per language, useful when you mix in groovy or jruby or scala…