foobar5512
foobar5512

Reputation: 2490

Null Pointer Exception at batch.begin() LibgDDX

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);
}

Upvotes: 1

Views: 531

Answers (2)

sandeep kundliya
sandeep kundliya

Reputation: 961

batch=new spritebatch();

and your problem is sloved.

Upvotes: 0

Daahrien
Daahrien

Reputation: 10320

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.
}

...

Upvotes: 1

Related Questions