Hi! I’m trying to create an external card class but I’m getting an error.
On my thoughts the problem is in myBatch, line 64
Main class
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
public class Main extends ApplicationAdapter {
SpriteBatch myBatch;
ShapeRenderer sr;
int tileSize = 32;
Card card1;
Texture tex;
@Override
public void create () {
myBatch = new SpriteBatch();
sr = new ShapeRenderer();
tex = new Texture(Gdx.files.internal("ball.png"));
card1 = new Card(tex);
}
public void grid(){
int height = Gdx.app.getGraphics().getHeight();
int width = Gdx.app.getGraphics().getWidth();
for (int i = 0; i <= width; i = i + tileSize) {
sr.rectLine(i, height, i, 0,2, Color.BLACK, Color.BLACK);
}
for (int j = 0; j <= width; j = j + tileSize){
sr.rectLine(width, j, 0, j,2, Color.BLACK, Color.BLACK);
}
}
@Override
public void render () {
Gdx.gl.glClearColor(20/255f, 40/255f, 100/255f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
myBatch.begin();
card1.draw(myBatch);
sr.begin(ShapeRenderer.ShapeType.Filled);
grid();
sr.end();
myBatch.end();
}
@Override
public void dispose () {
sr.dispose();
}
}
Card class
package com.mygdx.game;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
public class Card extends Sprite {
public Card(Texture myTexture) {
//myTexture = new Texture(Gdx.files.internal("ball.png"));
}
}
An error:
Exception in thread “LWJGL Application” java.lang.NullPointerException
at com.badlogic.gdx.graphics.g2d.SpriteBatch.flush(SpriteBatch.java:962)
at com.badlogic.gdx.graphics.g2d.SpriteBatch.end(SpriteBatch.java:183)
at com.mygdx.game.Main.render(Main.java:68)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
Process finished with exit code 0