Search code examples
libgdxrenderingtexturesspritebatch

SpriteBatch.draw(Sprite, ...) throws NullPointerException in LibGDX


The title is self-explanatory... Apparently the problem is in the flush method of the SpriteBatch class on the line where it says lastTexture.bind();. I did some surfing and found out it maybe because the sprite doesn't have a texture. So I ran some tests with if-statements and I literally wrote -

if(sprite.getTexture() != null) {
   sb.draw(sprite, ...);
}

And it still went to the draw line and then threw this error! Any help would be appreciated...


Solution

  • I found out what was wrong. The problem was actually with my box textures which I was rendering before the player. Since they were stored in the lastTexture variable in my SpriteBatch while rendering the player, I got mislead and was trying to debug the wrong code. Hope this helps if anyone else faces the same problem!