Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

LibGdx Exception

$
0
0

Here is the exception:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.mygdx.test.excav.game.WorldRender.renderTest(WorldRender.java:34)
at com.mygdx.test.excav.game.WorldRender.render(WorldRender.java:28)
at com.mygdx.test.excav.MainGame.render(MainGame.java:41)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

Relevant Code:

public class MainGame extends ApplicationAdapter {

    private static final String TAG = MainGame.class.getName();
    private WorldInit worldInit;
    private WorldRender worldRender;
    private boolean paused;

    @Override
    public void create () {

        Gdx.app.setLogLevel(Application.LOG_DEBUG);

        worldInit = new WorldInit();
        worldRender = new WorldRender(worldInit);

        paused = false;
    }

    @Override
    public void render () {
        if (!paused) {
            worldInit.update(Gdx.graphics.getDeltaTime());
        }
        Gdx.gl.glClearColor(0x64/255.0f, 0x95/255.0f, 0xed/255.0f, 0xff/255.0f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        worldRender.render();
    }

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

    @Override
    public void pause () {
        paused = true;
    }

    @Override
    public void resume () {
        paused = false;
    }

    @Override
    public void dispose () {
        worldRender.dispose();
    }
}

public class WorldInit {
    private static final String TAG = WorldInit.class.getName();
    public Sprite[] testSprites;
    public int curSprite;

    public WorldInit () {
        init();
    }

    private void init () {
        initTest();
    }

    private void initTest() {
        testSprites = new Sprite[5];

        int width = 32;
        int height = 32;

        Pixmap pixmap = createPixmap(width, height);
        Texture texture = new Texture(pixmap);

        for (int i = 0; i < testSprites.length; i++) {
            Sprite spr = new Sprite(texture);
            spr.setSize(1, 1);
            spr.setOrigin(spr.getWidth() / 2.0f, spr.getHeight() / 2.0f);
            // Calculate random position for sprite
            float randomX = MathUtils.random(-2.0f, 2.0f);
            float randomY = MathUtils.random(-2.0f, 2.0f);
            spr.setPosition(randomX, randomY);
            // Put new sprite into array
            testSprites[i] = spr;
        }
        // Set first sprite as selected one
        curSprite = 0;
    }

    private Pixmap createPixmap(int width, int height) {
        Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
        pixmap.setColor(1, 0, 0, .5f);
        pixmap.fill();
        pixmap.setColor(1, 1, 0, 1);
        pixmap.drawLine(0, 0, width, height);
        pixmap.drawLine(width, 0, 0, height);
        pixmap.setColor(0, 1, 1, 1);
        pixmap.drawRectangle(0, 0, width, height);
        return pixmap;
    }

    public void update (float deltaTime) {
        updateTest(deltaTime);
    }

    private void updateTest(float deltaTime) {
        float rotation = testSprites[curSprite].getRotation();
        rotation += 90 * deltaTime;
        rotation %= 360;
        testSprites[curSprite].setRotation(rotation);
    }
}

public class WorldRender implements Disposable {
    private OrthographicCamera camera;
    private SpriteBatch batch;
    private WorldInit worldInit;

    public WorldRender(WorldInit worldinit) {
        this.worldInit = worldInit;
        init();
    }

    private void init() {
        batch = new SpriteBatch();
        camera = new OrthographicCamera(Constant.VIEWPORT_WIDTH,
        Constant.VIEWPORT_HEIGHT);
        camera.position.set(0, 0, 0);
        camera.update();
    }

    public void render() {
        renderTest();   
    }

    private void renderTest() {
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        for(Sprite sprite : worldInit.testSprites) {
            sprite.draw(batch);
        }
        batch.end();
    }

    public void resize(int width, int height) {
        camera.viewportWidth = (Constant.VIEWPORT_HEIGHT / height) *
        width;
        camera.update();
    }

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

Viewing all articles
Browse latest Browse all 434

Trending Articles