0

I'm having some trouble with setting texture region to my TiledMapTile. It gives me NullPointerException and I have no idea why.

Here is some code:

TiledMapTile coinTile;

public void show () {
    mapTexture1 = new Texture(Gdx.files.internal("maps/other/texture1.png"));

    TextureRegion mapTexture1Region = new TextureRegion(mapTexture1, 32, 0, 16, 16);
    coinTile.setTextureRegion(mapTexture1Region);
}

And here is the error:

04-09 21:57:18.222: E/AndroidRuntime(7792): java.lang.NullPointerException
04-09 21:57:18.222: E/AndroidRuntime(7792): at com.never.mind.screens.GameScreen.show(GameScreen.java:225)

which leads to this line:

coinTile.setTextureRegion(mapTexture1Region);
Qiu
  • 5,651
  • 10
  • 49
  • 56
urban07
  • 61
  • 10
  • 1
    Can you post the whole stacktrace. Also, where is mapTexture1 defined? – Alastair McCormack Apr 09 '15 at 19:48
  • Updated, as you asked(I'm new to java - I assume that by stacktrace you meant the exact error and line of code?) – urban07 Apr 09 '15 at 20:01
  • Run up a quick debug log to check that `Gdx.files.internal("maps/other/texture1.png")` is returning something other than `null` – Alastair McCormack Apr 09 '15 at 20:22
  • I didn't get anything about this texture - only got this: Thread [<9> GLThread 10] (Suspended (exception NullPointerException)) GLSurfaceView$GLThread.run() line: 1122 – urban07 Apr 09 '15 at 20:31

1 Answers1

0

Usually, a NullPointerException happens when you did not initialize something in your code.

Since you did not provide your full code, I could only give your suggestions. Like what you did

mapTexture1 = new Texture(Gdx.files.internal("maps/other/texture1.png"));

You should also

coinTile = new TiledMapTile () {
@Override
public int getId() {
    return 0;
}

@Override
public void setId(int id) {

}

@Override
public BlendMode getBlendMode() {
    return null;
}

@Override
public void setBlendMode(BlendMode blendMode) {

}

@Override
public TextureRegion getTextureRegion() {
    return null;
}

@Override
public void setTextureRegion(TextureRegion textureRegion) {

}

@Override
public float getOffsetX() {
    return 0;
}

@Override
public void setOffsetX(float offsetX) {

}

@Override
public float getOffsetY() {
    return 0;
}

@Override
public void setOffsetY(float offsetY) {

}

@Override
public MapProperties getProperties() {
    return null;
}

}

By the way read the API for spcific paremeters.

Since you are new to java read here to get a grasp of NullPointers

Community
  • 1
  • 1
Fish
  • 1,689
  • 1
  • 18
  • 28
  • Thanks. When I added the code you posted, it stopped returning nullpointerexception in coinTile. However, now it gives nullpointerexception when I try to update null Array with any rectangle :/ I'll mark your answer as solution when I figure out how to fix it and make sure it works :) – urban07 Apr 10 '15 at 19:53
  • Please open a new question topic as ask that other question. Provide as much information as you can possibly provide in order to obtain best results on stack overflow – Fish Apr 10 '15 at 21:35