# Why do I get this file loading exception when trying to draw sprites with libgdx?

I'm having trouble with the "Drawing Images" section on the libgdx tutorial. I set up the documents completely and I typed the code as follows:

public class Game implements ApplicationListener {
public static final String LOG = Game.class.getSimpleName();
private FPSLogger fpsLogger;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
private TextureRegion region;

//removed irrelevant code for this question...

@Override
public void render() {
texture = new Texture(Gdx.files.internal("android.png"));
region = new TextureRegion(texture, 20, 20, 50, 50);
sprite = new Sprite(texture, 20, 20, 50, 50);
sprite.setPosition(10, 10);
sprite.setRotation(45);

Gdx.gl.glClearColor(0f, 1f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(texture,10,10);
batch.draw(region,10,10);
sprite.draw(batch);
batch.end();

// output the current FPS
fpsLogger.log();
}
}


I went through the tutorial on the website but when I run the code I get errors:

Exception in thread "LWJGL Application"
com.game.Game.render(Game.java:46)    at


I set the android.png in my assests folder in my android project linking it to the desktop one, I don't understand what I'm doing wrong. What is making these errors?

• You should clean up the code you post to just the relevant code. It's not too bad in this situation, since the program is pretty small. But it's a bad habit to get into. – MichaelHouse Mar 26 '12 at 16:11
• Yea, but how would I determine which is important and which isn't? When there is more complicated code wouldn't there be more to explain? – Blue Mar 26 '12 at 16:43
• If you don't know what the important part of the code is, you need to understand the code better. Check out the changes I made as an example of how you might cull your code. Like I said it's not too big of an issue this time. Just wanted to let you know. – MichaelHouse Mar 26 '12 at 16:52

The relevant part of that stack trace appears to be the message about not being able to find the android.png file. Since you assert the file does exist on-disk, the problem is likely an issue with the working directory of your application at the time you attempt to load the file and the file's location relative to that working directory.

I'm not entirely sure if this is true of Java applications, but the working directory is generally going to default to that of the application itself or the working directory of the shell that invoked the application (if launching it from a console). So, if your image is in an "assets" subfolder relative to the application, you'd have to attempt to load "assets/android.png" and not just "android.png".

Also note that because the image appears in an assets subfolder in your project structure, that does not necessarily mean it will be deployed with the final built application unless you've configured your IDE/toolchain (which you don't specify) to do so.

In a pinch, you may be able to use the getAbsoluteFile method of the File object to print the absolute path of the image at runtime to the console or in your debugger. That may help you diagnose where your program expects the image to be versus where it actually is.

The real problem to the code you posted above is these three lines:

texture = new Texture(Gdx.files.internal("android.png"));
region = new TextureRegion(texture, 20, 20, 50, 50);
sprite = new Sprite(texture, 20, 20, 50, 50);


You are instantiating these things in the render loop which is a really bad idea. @Override the create method and create them in there.

Here is a good reference: http://code.google.com/p/libgdx/wiki/ApplicationLifecycle

• Even though it's not an answer to the question at hand, +1 for pointing out the texture creation before that became his next question =) – Patrick Hughes Mar 27 '12 at 0:20

I encountered the same problem. (I am using eclipse indigo on ubuntu 10.04). The libgdx project setup tool creates android and desktop projects The asset folder in desktop project is a link to android project assets folder When you add a new file to the assets folder ,it takes some time for eclipse to register that a new file is added,as such FileNotFoundError is thrown. Manualy refreshing the linked folder from eclipse resolved the problem for me.

Hope this helps others.

• This worked for me too. Really strange error. – boolean Apr 22 '13 at 3:21

I had a similar problem and I decided to try taking assets/filename.png and shortening it to just filename.png and that fixed my loading issue. I had to manually refresh as well. Thanks for the tip David Dimalanta. :)

"Sometimes, you don't need to write "assets/android.png". "android.png" is fine but make sure that the name of the file and the file extention type (i.e. PNG, JPEG, TXT) are matched and correct." - That's the line that did it for me right there.

Sometimes, you don't need to write "assets/android.png". "android.png" is fine but make sure that the name of the file and the file extention type (i.e. PNG, JPEG, TXT) are matched and correct. Also, it is better to put it only at the create() method (show() method if you're implementing Screen) even the sprite and region if you're initializing it to prevent from getting a Nullpointer Exception error message.