Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am getting a null pointer exception using libGDX that the debugger points as the SpriteBatch.end() line. I was wondering what would cause this.

Here is the offending code block, specifically the batch.end() line:

    batch.begin();

    for (int j = 0; j < 3; j++)
        for (int i = 0; i < 3; i++)
            if (zoomgrid[i][j].getPiece().getImage() != null)
                zoomgrid[i][j].getPiece().getImage().draw(batch);

    batch.end();

The top of the stack is actually a line that calls

lastTexture.bind();

In the flush() method of com.badlogic.gdx.graphics.g2d.SpriteBatch.

I appreciate any input, let me know if I haven't included enough information.

share|improve this question
    
What type is getImage()? –  XiaoChuan Yu Oct 19 '13 at 3:59
    
getImage() returns the Sprite member in my Piece class. –  odaymichael Oct 19 '13 at 11:29
    
How do you set up your Sprite (presuming it's the LibGDX Sprite class)? Sounds a bit like the texture might be null. –  ThorinII Oct 20 '13 at 22:09
    
I do believe that is it. I was instantiating with new Sprite(),but the draw() method was being called before the texture was actually loaded. Thanks for the response. –  odaymichael Oct 21 '13 at 1:57

1 Answer 1

The fact that the exception points to a line in the callstack that reads

lastTexture.bind();

allows you to easily determine the cause of the exception (assuming the debug information in accurate and the call stack and exception source data is correct, which is probably a safe assumption in this case).

The only thing that be null in that line is lastTexture. That itself implies that the problem is that you've failed to initialize the sprite object correct, possibly failing to do something that sets up its backing texture (so it's null when you go to render the batch).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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