I started trying to create a TestGame. Currently having an NPE and after spending hours on google and trying everything I could think of I can't seem to understand what I'm overlooking or forgetting. I had everything in one class and all worked perfectly. Though I wanted to create a GUI where I could switch between a MainMenu and PlayScreen. My MainMenu class works fine, though when I hit my neat little "play" button to enter the PlayScreen class I get these errors:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.me.game.PlayScreen.render(PlayScreen.java:90)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.me.game.TestGame.render(TestGame.java:27)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:214)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Playscreen error:
batch.draw(player.getCurrentFrame(), player.getPosition().x , player.getPosition().y);
Game error:
if (screen != null) screen.render(Gdx.graphics.getDeltaTime());
TestGame error:
super.render();
TestMenu class (Game class)
public class TestGame extends Game {
Game game;
@Override
public void create() {
game = this;
setScreen(new MainMenu(game));
public void render() {
super.render();
}
MainMenu class
public class MainMenu implements Screen{
SpriteBatch batch;
Game game;
Stage stage;
public MainMenu(Game game){
this.game = game;
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
batch.begin();
stage.draw();
batch.end();
}
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
game.setScreen(new PlayScreen(game));
stage.clear();
return true;
PlayScreen
public class PlayScreen implements Screen {
SpriteBatch batch;
Player player;
Game game;
Texture playerTexture;
public PlayScreen(Game game){
this.game = game;
}
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(player.getCurrentFrame(), player.getPosition().x , player.getPosition().y);
}
player.update();
batch.end();
}
public void show() {
batch = new SpriteBatch();
Player class
public class Player {
Texture playerTexture ;
Vector2 position;
String textureLoc;
public Player(Vector2 position, String textureLoc){
this.position = position;
movement = "";
playerTexture = new Texture(Gdx.files.internal("Character.png"));
public String getTextureLoc() {
return textureLoc;
}
public void setTextureLoc(String textureLoc) {
this.textureLoc = textureLoc;
}
The most helpful was Libgdx Screen NullPointerException when referencing another class where the problem also seems to be that I didn't initialize player in the PlayerScreen class at the show method. Though when I do fill player = new Player(position, textureLoc); in under the show method
public void show() {
batch = new SpriteBatch();
player = new Player(position, textureLoc);
I get other errors
at com.me.game.Player.<init>(Player.java:50)
at com.me.game.PlayScreen.show(PlayScreen.java:90)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.me.game.MainMenu$1.touchDown(MainMenu.java:90)
Which point to:
bounds = new Rectangle(position.x, position.y, currentFrame.getRegionWidth(), currentFrame.getRegionHeight());
player = new Player(position, textureLoc);
game.setScreen(new PlayScreen(game));
Though I assume it has something to do with the player not being initialized correctly? Anyhow for some reason I just can't seem to solve this. Any ideas would be greatly appreciated.