我是說我想創建我的球員,他的動畫一個NullPointerException動畫。我真的不明白什麼是錯的。 下面是代碼:NullPointerException異常是Java的嘗試與libgdx
package com.sheep.game.Sprites;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.sheep.game.Screens.PlayScreen;
public class Player extends Sprite {
float elapsedTime = 0;
public Animation<TextureRegion> animation;
public Texture sheepLoop2;
TextureRegion[] animationFrames;
public Sprite spr_player;
private PlayScreen screen;
public Player(PlayScreen screen) {
this.screen = screen;
sheepLoop2 = new Texture("sheepLoop2.png");
TextureRegion[][] frames = TextureRegion.split(sheepLoop2, 33, 21);
animationFrames = new TextureRegion[2];
int index = 0;
for(int i = 0; i < 1; i++) {
for(int j = 0; j < 1; j++) {
animationFrames[index++] = frames[j][i];
}
}
animation = new Animation<TextureRegion>(1f/30f, animationFrames);
spr_player = new Sprite(sheepLoop2);
}
public void update() {
spr_player.setX(10);
spr_player.setY(100);
spr_player.setRegion(animation.getKeyFrame(elapsedTime,true));
}
public void render() {
elapsedTime += Gdx.graphics.getDeltaTime();
}
public Texture getSheepLoop2() {
return sheepLoop2;
}
}
而且堆棧跟蹤是:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion(TextureRegion.java:112)
at com.sheep.game.Sprites.Player.update(Player.java:53)
at com.sheep.game.Screens.PlayScreen.update(PlayScreen.java:61)
at com.sheep.game.Screens.PlayScreen.render(PlayScreen.java:84)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.sheep.game.SheepGame.render(SheepGame.java:28)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
的可能的複製[什麼是空指針異常,怎麼解決呢?(HTTP://計算器.COM /問題/ 218384 /什麼 - 是 - 一 - NullPointerException異常和知識-DO-I-FIX-IT) – csmckelvey
'異常在線程com.sheep.game.Sprites 「LWJGL應用」 顯示java.lang.NullPointerException。 Player.update(Player.java:53)'哪一行是53?那裏可能是空的?當你在該區域進行調試時,你會發現什麼? – csmckelvey
檢查animationFrame是否已正確初始化。 – Serhiy