每当我运行程序时,我都会收到此错误:
Exception in thread "Thread-2" java.lang.NullPointerException
at ca.wamlab.game.entities.Player.tick(Player.java:20)
at ca.wamlab.game.level.Level.tick(Level.java:38)
at ca.wamlab.game.Game.tick(Game.java:169)
at ca.wamlab.game.Game.run(Game.java:112)
at java.lang.Thread.run(Thread.java:744)
忽略除了第一个之外的所有因素,因为所有这些都是因为那个。
我在以下代码中收到了该错误:
package ca.wamlab.game.entities;
import ca.wamlab.game.InputHandler;
import ca.wamlab.game.gfx.Colors;
import ca.wamlab.game.gfx.Screen;
import ca.wamlab.game.level.Level;
public class Player extends Mob{
private InputHandler input;
private int color = Colors.get(-1, 111, 145, 543);
public Player(Level level, int x, int y, InputHandler input) {
super(level, "Player", x, y, 1);
}
public void tick() {
int xa = 0;
int ya = 0;
if(input.up.isPressed()){
ya--;
}
if(input.down.isPressed()){
ya++;
}
if(input.right.isPressed()){
xa++;
}
if(input.left.isPressed()){
xa--;
}
if (xa != 0 || ya != 0){
move(xa, ya);
isMoving = true;
}else{
isMoving = false;
}
}
public void render(Screen screen) {
int xTile = 0;
int yTile = 28;
int modifier = 8*scale;
int xOffset = x - modifier/2;
int yOffset = y - modifier /2-4;
screen.render(xOffset, yOffset, xTile + yTile * 32, color);
screen.render(xOffset + modifier, yOffset, (xTile + 1) + yTile * 32, color);
screen.render(xOffset, yOffset + modifier, xTile + (yTile + 1) * 32, color);
screen.render(xOffset + modifier, yOffset + modifier, (xTile + 1) + (yTile + 1) * 32, color);
}
public boolean hasCollided(int xa, int ya) {
return false;
}
}
输入类是:
package ca.wamlab.game;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class InputHandler implements KeyListener{
public InputHandler(Game game) {
game.addKeyListener(this);
}
public class Key {
private int numTimesPressed;
private boolean pressed = false;
public int getNumTimesPressed(){
return numTimesPressed;
}
public boolean isPressed(){
return pressed;
}
public void toggle(boolean isPressed){
pressed = isPressed;
if(isPressed)numTimesPressed++;
}
}
public Key up = new Key();
public Key down = new Key();
public Key right = new Key();
public Key left = new Key();
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
toggleKey(e.getKeyCode(), true);
}
public void keyReleased(KeyEvent e) {
toggleKey(e.getKeyCode(), false);
}
public void toggleKey(int keyCode, boolean isPressed){
if(keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_UP){ up.toggle(isPressed);}
if(keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN){ down.toggle(isPressed);}
if(keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT){ right.toggle(isPressed);}
if(keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT){left.toggle(isPressed);}
}
}
我知道我得到这个是因为代码中的某些内容被设置为 null (我认为它是ispressed()
),但我无法想办法解决它......
答案 0 :(得分:2)
InputHandler类中的上,下,右,左实例变量用
初始化public Key up = new Key();
public Key down = new Key();
public Key right = new Key();
public Key left = new Key();
如果input是InputHandler的引用变量并且InputHandler的对象被赋予它,那么基于上面的代码,up,down,right和left永远不能为null。因此,基于确认第20行if(input.up.isPressed()){
,最可能input
为空。
如果您可以提供InputHander input
为其分配对象的代码(如果有的话),它将帮助我们。代码中的哪个键监听器使用addKeyListenter()
方法注册?
并且提供的tick()
方法属于哪个类?
从stacktrace可以看出,所有三个Player,Level和Game类都有同名的方法tick()
at ca.wamlab.game.entities.Player.tick(Player.java:20)
at ca.wamlab.game.level.Level.tick(Level.java:38)
at ca.wamlab.game.Game.tick(Game.java:169)
许多重要的细节都不为人知。更多细节会有所帮助。