0

Null Pointer Exception when using unProject in Libgdx

I have a smasher game project and having problem when the game switches from MainMenuScreen to MainGameScreen, because of camera.unproject. Texture img; Array<Rectangle> listBalloon; long lastSpawnTime; OrthographicCamera camera; BalloonPopper game; public MainGameScreen(BalloonPopper game) { this.game = game; listBalloon = new Array<Rectangle>(); spawnBalloon(); } @Override public void show() { img = new Texture("testBalloon.png"); } @Override public void render(float delta) { ScreenUtils.clear(1, 0, 0, 1); game.batch.begin(); for(Rectangle blns: listBalloon) { game.batch.draw(img, blns.x, blns.y, blns.width,blns.height); } game.batch.end(); if(TimeUtils.nanoTime() - lastSpawnTime > 1000000000) spawnBalloon(); for(Iterator<Rectangle> iter = listBalloon.iterator(); iter.hasNext();) { Rectangle blns = iter.next(); blns.y += 200 * Gdx.graphics.getDeltaTime(); if(blns.y > 720) iter.remove(); if(Gdx.input.isTouched()) { Vector3 blnXY = new Vector3(); blnXY.set(Gdx.input.getX(), Gdx.input.getY(), 0); camera.unproject(blnXY); if(blns.contains(blnXY.x, blnXY.y)) { iter.remove(); } } } } public void spawnBalloon() { Rectangle bln = new Rectangle(); bln.x = MathUtils.random(0, 480 - 64); bln.y = -20; bln.width = 64; bln.height = 128; listBalloon.add(bln); lastSpawnTime = TimeUtils.nanoTime(); } In for(Iterator<Rectangle> iter = listBalloon.iterator(); iter.hasNext();) in render(), if I remove the camera.unproject, the program will move to MainGameScreen with no problem. However, sometimes clicking the spawned texture won't remove them and i have to click like a bit above of the texture so it can be removed. So the only thing i know to make it precisely find the location when clicking, is by adding camera.unproject. But yeah the problem is the null pointer exc

13th Jun 2021, 3:19 AM
M.O.HONOR
M.O.HONOR - avatar
2 Antworten
0
Hello M.O.HONOR I am not familiar with libgdx but I know Nullpointerexeptions ;) The best is to use debug mode in your ide and run the code step by step to see which variable returns null. Did you initialize camera? OrthographicCamera camera; //default value: null camera.anyMethod() will throw a Nullpointerexeption.
13th Jun 2021, 8:10 AM
Denise Roßberg
Denise Roßberg - avatar
0
perhaps it is better to ask here https://libgdx.com/community/discord/
13th Jun 2021, 7:00 AM
Ciro Pellegrino
Ciro Pellegrino - avatar