libgdx: can't find region in atlas

So, I am trying to read images off an atlas in libGDX. I have packed my images into a .atlas file and a .png file already, and I am reading them like this:


TextureRegion player = getAtlas().findRegion("player-model-fiona");				

Error code:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.NullPointerException
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:111)
Caused by: java.lang.NullPointerException
	at com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable.setRegion(TextureRegionDrawable.java:46)
	at com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable.<init>(TextureRegionDrawable.java:32)
	at com.devhub.improvgames.magicgame.StartGameScreen.updateValues(StartGameScreen.java:152)
	at com.devhub.improvgames.magicgame.StartGameScreen.show(StartGameScreen.java:126)
	at com.badlogic.gdx.Game.setScreen(Game.java:59)
	at com.devhub.improvgames.magicgame.MagicGame.setScreen(MagicGame.java:127)
	at com.devhub.improvgames.magicgame.MenuScreen$1.touchUp(MenuScreen.java:30)
	at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:43)
	at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:309)
	at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:297)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:186)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:108)

The specific error happens at

playerModelImage.setDrawable(new TextureRegionDrawable(player));

because I am trying to set the drawable to a textureregiondrawable that has a null argument.
So, basically it can’t find the region, making player null. I have no idea why :frowning:
Manually getting the images (manually setting x/y/width/height) works though. I’m probably too vague and this is probably some obscure but easy to solve bug but I’m stumped.

pages.png (source pic for atlas), test pics obviously

pack file:


pages.png
format: RGBA8888
filter: Nearest,Nearest
repeat: none
player-model-aidan
  rotate: false
  xy: 1, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
player-model-azrael
  rotate: false
  xy: 25, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
player-model-raven
  rotate: false
  xy: 25, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
robe-adept
  rotate: false
  xy: 25, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
robe-god
  rotate: false
  xy: 25, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
staff-master
  rotate: false
  xy: 25, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
staff-neophyte
  rotate: false
  xy: 25, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
player-model-elysia
  rotate: false
  xy: 49, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
robe-neophyte
  rotate: false
  xy: 49, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
staff-god
  rotate: false
  xy: 49, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
player-model-fiona
  rotate: false
  xy: 73, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
robe-master
  rotate: false
  xy: 73, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
staff-epic
  rotate: false
  xy: 73, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
player-model-raiden
  rotate: false
  xy: 97, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
robe-epic
  rotate: false
  xy: 97, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1
staff-adept
  rotate: false
  xy: 97, 1
  size: 22, 28
  orig: 22, 28
  offset: 0, 0
  index: -1

Previously I had problem with atlas too when upgrading to 0.9.7. It’s not “-” but “_”. My quick fix was removing the underscore. Maybe you can try it.

player-model-fiona is in your pack file, should work. Refresh Eclipse, maybe it isn’t picking up the latest files if you are loading it through the classpath. I use a -Declipse flag when running in Eclipse (in the vm arguments for your run configuration), then:

		if (System.getProperty("eclipse") != null)
			skin = new Skin(Gdx.files.internal("../assets/skin/skin.json"));
		else
			skin = new Skin(Gdx.files.internal("skin/skin.json"));

The same could be done to load your atlas. This way I load directly from disk when running in Eclipse but load from the classpath otherwise without changing any code. Note Gdx.files.internal checks on disk for the path first, then the classpath. You could also use Gdx.files.absolute, etc.

So it should be player_model_fiona? And I have to change the atlas file too, right?
EDIT: doesn’t seem to work. Tried all combinations of the atlas file and the code between “-” and “_”.

@Nate the file is the latest, I can view it through eclipse and it is the correct version.
I am currently loading the atlas like so:

public TextureAtlas getAtlas() {
		if (atlas == null) {
			atlas = new TextureAtlas(Gdx.files.internal("image-atlases/pages.atlas"));		
		}
		return atlas;
	}

EDIT: I think you may be on to something, I replaced my working pack file from steigert with the error one and it still worked, I tried running with -Declipse but I get

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Cannot open a stream to a directory: C:\Users\Jimmt\Documents\Jimmt\EclipseWorkspace\MagicGame-Android\assets\image-atlases (Internal)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:111)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Cannot open a stream to a directory: C:\Users\Jimmt\Documents\Jimmt\EclipseWorkspace\MagicGame-Android\assets\image-atlases (Internal)
	at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:134)
	at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:97)
	at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:222)
	at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:217)
	at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:207)
	at com.devhub.improvgames.magicgame.StartGameScreen.updateValues(StartGameScreen.java:141)
	at com.devhub.improvgames.magicgame.StartGameScreen.show(StartGameScreen.java:127)
	at com.badlogic.gdx.Game.setScreen(Game.java:59)
	at com.devhub.improvgames.magicgame.MagicGame.setScreen(MagicGame.java:127)
	at com.devhub.improvgames.magicgame.MenuScreen$1.touchUp(MenuScreen.java:30)
	at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:43)
	at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:309)
	at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:297)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:186)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:108)
Caused by: java.io.FileNotFoundException: C:\Users\Jimmt\Documents\Jimmt\EclipseWorkspace\MagicGame-Android\assets\image-atlases (Access is denied)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(Unknown Source)
	at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:131)

Yay, it works(I think)
Used Nate’s method of

 if (System.getProperty("eclipse") != null)
         skin = new Skin(Gdx.files.internal("../assets/skin/skin.json"));
      else
         skin = new Skin(Gdx.files.internal("skin/skin.json"));

and the -Declipse vm flag
Not sure if it’s good to use an absolute path like that though…

No I meant remove _ and -. Glad you made it. I never countered this before, as I always remember to refresh the android project.