Daniel Darius
Daniel Darius

Reputation: 45

Libgdx NullPointerException

I am trying to make a simple animation in libgdx with a sprite sheet, a litle man who's moving his legs. This is the code:

package com.pro.schoolrun;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class SchoolRun implements ApplicationListener {
    Texture player;
    TextureRegion[] regions;
    SpriteBatch batch;
    OrthographicCamera camera;
    int i;

    @Override
    public void create() {

        i = 0;

        player = new Texture(Gdx.files.internal("data/spritesheet.png"));
        regions[0] = new TextureRegion(player, 0, 0, player.getWidth() / 4,
                player.getHeight() / 2);
        regions[1] = new TextureRegion(player, 64, 0, player.getWidth() / 4,
                player.getHeight() / 2);
        regions[2] = new TextureRegion(player, 128, 0, player.getWidth() / 4,
                player.getHeight() / 2);
        regions[3] = new TextureRegion(player, 192, 0, player.getWidth() / 4,
                player.getHeight() / 2);
        regions[4] = new TextureRegion(player, 0, 128, player.getWidth() / 4,
                player.getHeight() / 2);
        regions[5] = new TextureRegion(player, 64, 128, player.getWidth() / 4,
                player.getHeight() / 2);
        regions[6] = new TextureRegion(player, 128, 128, player.getWidth() / 4,
                player.getHeight() / 2);
        regions[7] = new TextureRegion(player, 192, 128, player.getWidth() / 4,
                player.getHeight() / 2);

        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 400);
        batch = new SpriteBatch();

    }

    @Override
    public void dispose() {
        player.dispose();
        batch.dispose();

    }

    @Override
    public void render() {

        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        camera.update();

        batch.setProjectionMatrix(camera.combined);

        while (true) {

            batch.begin();
            if (i > 7) {
                i = -1;
                i++;
            } else {
                i++;
            }

            batch.draw(regions[i], Gdx.graphics.getWidth() / 2,
                    Gdx.graphics.getHeight() / 2);
           batch.end();
        }

    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

But when I run this on android it gives me a NullPointer Exception at line 24(at regions[0]). This is the MainActivity class:

package com.pro.schoolrun;

import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;

public class MainActivity extends AndroidApplication {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = true;
        cfg.useAccelerometer=false;
        cfg.useCompass = false;


        initialize(new SchoolRun(), cfg);
    }
}

Upvotes: 0

Views: 2333

Answers (4)

Fish
Fish

Reputation: 1697

Your NullPointerException occurs when the image file is null. (Meaning there is no image file found in this directory.)

Make sure your image is found at the right location

In your case, your image should be located at

"directory containing your project"/SchoolRun/android/assets/data

or you change your code to

        player = new Texture(Gdx.files.internal("spritesheet.png"));

Hope you succeed :)

Upvotes: 1

Zohaib Amir
Zohaib Amir

Reputation: 3562

NullPointer exception means you are trying to use something that has not been initialized yet. Objects, arrays and variables must be initialized/assigned a value before they can be used.

In your case, you are trying to use a TextureRegion Array named "regions" but you haven't initialized yet. You can initialize array like this:

region = new TextureRegion[8];

where 8 is the size of the array. This statement must be called BEFORE you use region.

Upvotes: 1

Kumar Saurabh
Kumar Saurabh

Reputation: 2305

public void create() {

region=new TextureRegion[8];
}

This should solve the issuse

Upvotes: 3

laalto
laalto

Reputation: 152787

You never initialize the regions array:

TextureRegion[] regions;

This should be something like

TextureRegion[] regions = new TextureRegion[8];

Upvotes: 8

Related Questions