1

I am trying to draw a .png to the screen, but I keep getting an error. The class I am working with is a subclass of Screen.

Here is the error I get:

FATAL EXCEPTION GLThread 1335 java.lang.NullPointerException  at SplashScreen.java.32

this line is:

batch.begin()

Here is my code inside of the Screen subclass:

private SpriteBatch batch;
private Texture splashTexture;
private Camera camera;


 final int CAMERA_WIDTH = Gdx.graphics.getWidth();
static final int CAMERA_HEIGHT = Gdx.graphics.getHeight();



@Override
public void render(float delta) {
   Gdx.gl.glClearColor(0, 0, 0, 1);
   Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
   batch.begin();
   batch.draw(splashTexture, 0, 0);
   batch.end();

}

@Override
public void resize(int width, int height) {
   // TODO Auto-generated method stub

}

@Override
public void show() {
   splashTexture = new Texture(Gdx.files.internal("splash.png"));
   camera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
    camera.position.set(CAMERA_WIDTH, CAMERA_HEIGHT, 0);
}
2
  • did you try to debug ? add the batch = new SpriteBatch(); to show() and make a breakpoint go on from there , i tried with exact same code and it works for me
    – idanakav
    Sep 16, 2012 at 16:54
  • Very weird, after I add a breakpoint to batch.begin(); it works?! Thanks
    – foobar5512
    Sep 16, 2012 at 17:23

2 Answers 2

1

Your SpriteBatch is null, you create your texture and your camera but you don't create the batch. the breakpoint you mention has nothing to do with it.

private SpriteBatch batch;
private Texture splashTexture;
private Camera camera;

...

@Override
public void show(){
    splashTexture = new Texture(Gdx.files.internal("splash.png"));
    camera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
    camera.position.set(CAMERA_WIDTH, CAMERA_HEIGHT, 0);

    batch = new SpriteBatch(); //create it like this.
}

...
0
batch=new spritebatch();

and your problem is sloved.

1
  • Worked for me. Just need to capitalize the S and B in SpriteBatch.
    – Bobby
    Jul 31, 2015 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.