libgdx - how do I load local assets without an android project?

Hi all,

I’ve been getting to grips with using libgdx to develop my game, but as I’m only creating this for desktop, I’m using a single project rather than the default split of base game / desktop project / android project. Now I’ve been trying to use the following line to load a textureatlas:

atlas = new TextureAtlas(Gdx.files.internal("assets/packed/game.atlas"));

where the ‘assets’ directory is in the root of the project directory. However running this code just gives me a NullPointerException. From what I can gather though, GDX.files.internal is expecting there to be an assets folder in the android project directory - which I’m not using.

So my question is, how can I correctly reference and load a local file in my project? Thanks.

The android project is necessary for this reason. Even if your not using it on android that is where your assets are stored. I’m not sure if there is a way around this however…

There are other methods on GDX.files as well though, I wondered if one of these can be used instead? (e.g. classpath(), but I’ve not figured out how)

It seems odd that I must have this separate android project folder simply to store my assets in when I’m not going to be using it.

Hey man.
Our big project is also desktop-only and uses libgdx - this shouldnt be a problem.
You seem to be doing it right - I suspect that you created the project with the libgdx gui ? which is nice and all IF you want those other ports… but if not, just make one yourself and add the jars
pretty sure it should work then.
also: if you wanna know where libgdx is really searching you can do sysout of Gdx.files.internal(“assets/packed/game.atlas”).file().absolutePath()

You can create a desktop-only project with GDX just like you would with any Java class library. Here’s how to do it in Eclipse.

  1. Create a new project.

  1. Right click your project and select Properties. In the “Java Build Path” tab, add the necessary GDX dependencies. In my project I copied them from the LibGDX Nightlies into the folder [icode]my_project/lib[/icode], then used Add JARs to select a JAR within the project path. If you want to keep your GDX JARs in the same folder for all your projects, you would select “Add External JARs” instead.

  1. Create a new source folder called “assets”, like so:

Inside that source folder, add a new folder like “res” or “data” for your resources. For example, if you have “assets/data” for your resources, then you’ll use the following to access it:
[icode]Gdx.files.internal(“data/myTex.png”)[/icode]

Another option is to include your resources in your “src” folder, but this is not always recommended.

  1. Compile and run a basic test:
    http://www.java-gaming.org/?action=pastebin&id=521

I respectfully disagree since resources/assets aren’t source files - but it works in both ways.
I have also no interest in packaging my resources into jars, so yeah…


http://imageshack.us/a/img844/7304/29974753.gif

old screenshot I made for a similar discussion, click to enlarge =P

edit: Gdx.files.internal(“content/loading_symbol.png”) works in this case, not sure what happens when you put it all in the src folder

Thanks all, that has helped me narrow things down to it being a problem only when I use TextureAtlas. My .atlas and .png file are in the same directory as I think libgdx expects, so I’m not sure what else could be wrong as I’m only seeing a generic NullPointerException.

which line ? you may be searching for a texture in the atlas which isn’t there. possible typo