MrWafles Dear
MrWafles Dear

Reputation: 1

Error with libGdx Screens

I have a problems when I try to compiled this is supuse that will a game.

Exception in thread "LWJGL Application" java.lang.NullPointerException at com.waflesgames.spaceinvader.Screens.PlayScreen.render(PlayScreen.java:35) at com.badlogic.gdx.Game.render(Game.java:46) at com.waflesgames.spaceinvader.SpaceInvader.render(SpaceInvader.java:24) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)

This is the PlayScreen.java

package com.waflesgames.spaceinvader.Screens;

import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.waflesgames.spaceinvader.SpaceInvader;

import javax.xml.soap.Text;

/**
* Created by Diego on 03/10/2015.
*/
public class PlayScreen implements Screen{

public SpaceInvader game;
Texture texture;
private OrthographicCamera gamecam;
private Viewport gamePort;
public PlayScreen(SpaceInvader Game){
    this.game = game;
    texture = new Texture("badlogic.jpg");
    gamecam = new OrthographicCamera();
    gamePort = new ScreenViewport(gamecam);
}

@Override
public void show() {

}

@Override
public void render(float delta) {
    game.batch.setProjectionMatrix(gamecam.combined);
    game.batch.begin();
    game.batch.draw(texture,0,0);
    game.batch.end();
}

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

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {

}
}

and this is the SpaceInvader.java

package com.waflesgames.spaceinvader;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.waflesgames.spaceinvader.Screens.PlayScreen;

public class SpaceInvader extends Game {
public  SpriteBatch batch;


@Override
public void create () {
    batch = new SpriteBatch();
    setScreen(new PlayScreen(this));
}

@Override
public void render () {
    super.render();
}
}

Upvotes: 0

Views: 126

Answers (1)

m.antkowicz
m.antkowicz

Reputation: 13571

you have error here:

    public PlayScreen(SpaceInvader Game){
        this.game = game;
        ...

it should be game instead of Game - you are get SpaceInvader instance to Game variable so below you are assigning the this.game to itself what causes nullpointer exception in render method (game is null so you cannot acces its batch)

Upvotes: 4

Related Questions