LibGdx Entity error

Hello!

I made player class.Player class controls player and sets to render the player.

public class Player extends ObjectClass{
	 Main1 game;
	 Texture brick;
	public void player() {
//Set Texture of player
	brick = new Texture(Gdx.files.internal("glass.png"));
	}
	public void render(SpriteBatch batch) {
		//start drawing brick
		batch.begin();
		batch.draw(brick,0,0);
		batch.end();
	}	
}

Then i set PlayState class to render that entity

public void draw() {
		//draw player
		player.render(batch);
	}

And then it all goes to main class where all of stuff renders :smiley:

public void render() {
            Gdx.gl.glClearColor(1, 1, 1, 1);
            Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
            camera.update();
            batch.setProjectionMatrix(camera.combined);
            gsm.update(Gdx.graphics.getDeltaTime()); // update
            gsm.draw(); // Draw all things in PlayState
	}

But when i want run the game, i just see NullPointerException and error Player.render(Player.java:30).That is that line

batch.draw(brick,0,0);

I really dont know what is problem.I had tried to fix it but really that error stays.
How can i fix that error?
Can someone help me :slight_smile:

you never Initialized the “batch” instance. You have to call [icode]batch = new SpriteBatch(…)[/icode] somewhere in your initialization code. :slight_smile:

Nop didn’t work. It just shows NullPoinertException

show us the create method of your libgdx game class

public class Main {

	public static void main(String[] args) {
		LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
		cfg.title = "Block";
		cfg.width = 700;
		cfg.height = 600;

		new LwjglApplication(new Main1(), cfg);
	}
}

Ok there is da code

Uh, that’s the desktop launcher. Try the create() method of the class that extends Game or implements ApplicationListener.

Ups soory this is it

public void create() {		
   
	 WIDTH = Gdx.graphics.getWidth(); // WIDTH
	 HEIGHT = Gdx.graphics.getHeight(); // HEIGHT
		batch = new SpriteBatch();
		
		setScreen(new PlayState(this));
	}

Null pointer exception means that the object you’re trying to access is null.

In other words, there is nothing there, therefore you’re getting the exception…

I would suggest you to look at exception the other way around. They are not here to be annoying. They are here to be helpful.

Imagine that you didn’t get the error just now, and instead nothing happened. I turned off exceptions in Eclipse at some time and I was so frustrated looking at code and why it didn’t work for hours… Without exceptions you would die… Or stop programming all together…

Make sure the texture is actually there?

It would throw a file not found exception.

@OP See when it gives you the error, copy and paste that entire thing here.

The problem is that you have not initialized brick and is null.

Change


public void player() {
//Set Texture of player
   brick = new Texture(Gdx.files.internal("glass.png"));
   }

with


public Player() {
//Set Texture of player
   brick = new Texture(Gdx.files.internal("glass.png"));
   }

Note that public void player() is not a constructor.

What Endos said.

Or you could call


Player player = new Player();
player.player();

somewhere in your other class, which looks funny :smiley:

thanks for help now it works :slight_smile: