Nullpointer exception at render

Hi, im starting with libgdx on android and im trying to do some practices, im getting this error

E/AndroidRuntime( 1592): FATAL EXCEPTION: GLThread
E/AndroidRuntime( 1592): java.lang.NullPointerException
E/AndroidRuntime( 1592): at gdx.game.Game.render(Game.java:81)
E/AndroidRuntime( 1592): at com.badlogic.gdx.backends.android.AndroidGrap
hics.onDrawFrame(AndroidGraphics.java:449)
E/AndroidRuntime( 1592): at com.badlogic.gdx.backends.android.surfaceview
.GLSurfaceViewCupcake$GLThread.guardedRun(GLSurfaceViewCupcake.java:713)
E/AndroidRuntime( 1592): at com.badlogic.gdx.backends.android.surfaceview
.GLSurfaceViewCupcake$GLThread.run(GLSurfaceViewCupcake.java:646)

Here is my code

public class Game implements ApplicationListener {

    Stage stage;
    SpriteBatch spriteBatch;
    muñeco pnj;
    Image image;
    Touchpad touchpad;
    TextureRegion[] walkFrames;
    float stateTime;
    OrthographicCamera camera;
    TextureRegion currentFrame;
    Label lbl;
    JoystickScreen js;

    public void create() {
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
        pnj = new muñeco(getSprites("data/sprite.png", 3, 4));
        pnj.setAnims();
        touchpad = new Touchpad(20, skin);
        touchpad.setBounds(15, 15, 100, 100);

        stage.addActor(touchpad);

    }

    public void render() {

        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        stage.act(Gdx.graphics.getDeltaTime());
        stateTime += Gdx.graphics.getDeltaTime();                       // #15
        if (touchpad.getKnobPercentX() > 50) {
            currentFrame = pnj.walkr.getKeyFrame(stateTime, true);      // #16
        }
        if (touchpad.getKnobPercentX() < -50) {
            currentFrame = pnj.walkl.getKeyFrame(stateTime, true);      // #16
        }
        if (touchpad.getKnobPercentY() > 50) {
            currentFrame = pnj.walku.getKeyFrame(stateTime, true);      // #16
        }
        if (touchpad.getKnobPercentY() < -50) {
            currentFrame = pnj.walkd.getKeyFrame(stateTime, true);      // #16
        }
        spriteBatch.begin();
        spriteBatch.draw(currentFrame, 100, 100);                         // #17
        spriteBatch.end();
        stage.draw();
    }

    public void resize(int width, int height) {
        stage.setViewport(width, height, true);
    }

    public void pause() {
    }

    public void resume() {
    }

    public void dispose() {
        stage.dispose();
    }

    public void escribir(String frase) {
        lbl.setText(frase);
    }

    public static TextureRegion[] getSprites(String filepath, int col, int fil) {
        Texture walkSheet = new Texture(Gdx.files.internal(filepath));
        TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth() / col, walkSheet.getHeight() / fil);
        TextureRegion[] walkFrames = new TextureRegion[col * fil];
        int index = 0;
        for (int i = 0; i < col-1; i++) {
            for (int j = 0; j < fil-1; j++) {
                walkFrames[index++] = tmp[i][j];
            }
        }
        return walkFrames;
    }
}

The line that gives the error is:

spriteBatch.begin();

Sorry for that dirty code, i have no idea of what im doing wrong but im sure its a really noob mistake.

Hope some help and sorry for my bad english

It doesn’t look like you are ever creating that SpriteBatch object, so the variable just hold a null value.

thanks that was a really stupid fail, but im still getting errors

E/AndroidRuntime( 1409): FATAL EXCEPTION: GLThread
E/AndroidRuntime( 1409): java.lang.NullPointerException
E/AndroidRuntime( 1409): at com.badlogic.gdx.graphics.g2d.SpriteBatch.dra
w(SpriteBatch.java:730)
E/AndroidRuntime( 1409): at gdx.game.Game.render(Game.java:67)
E/AndroidRuntime( 1409): at com.badlogic.gdx.backends.android.AndroidGrap
hics.onDrawFrame(AndroidGraphics.java:449)
E/AndroidRuntime( 1409): at com.badlogic.gdx.backends.android.surfaceview
.GLSurfaceViewCupcake$GLThread.guardedRun(GLSurfaceViewCupcake.java:713)
E/AndroidRuntime( 1409): at com.badlogic.gdx.backends.android.surfaceview
.GLSurfaceViewCupcake$GLThread.run(GLSurfaceViewCupcake.java:646)

on the next line
spriteBatch.draw(currentFrame, 100, 100);

that textureregion should be ok, the getSprites() function its almost copy paste from here: http://code.google.com/p/libgdx/wiki/SpriteAnimation

Really thanks for the help and sorry for my bad english

getSprites()?
In the code you posted before currentFrame isn’t being instantiated either.

Edit: by the way, you really should check your code an extra time before posting more of these NullPointerException errors, because they are often easy enough to spot,
make sure you have a clear idea where your variables is instantiated.

I dont know what happened when you give null TextureRegion for SpriteBatch to draw but since exception raised from inside, your currentFrame maybe the ebil.

NB: if you want quicker method to make this work, use that basic Game class instead ApplicationListener.

First, are you sure it compiled? muneco’s Animation fields aren’t public.

EDIT: You delete the post after I reply? :open_mouth:

I solved it myself and ofc it compiles with that code the currentframe only initializes when i drag the touchpad, i added a default frame and it works.

Thank u very much for the help i will be in this great forum, and again sorry for my poor english i try my best :slight_smile: