我有一个非常简单的程序,让敌人跟随玩家,谁可以在x和y轴上自由移动。问题是,每次我尝试执行player.getx()或player.gety()之类的操作时,它都会返回一个空指针异常。找到了另一个有这个问题的人,但没有解决我的问题。下面是我得到的信息:
public class SpaceObject {
public float x;
public float y;
protected float x2;
protected float y2;
public float getx() { return x; }
public float gety() { return y; }
}
//Here's my camera attempting to access player.getx() and player.gety(), returning with a null pointer exception:
camera.position.set(player.getx(), player.gety(), 0);
// Here's an example of a bullet-enemy detection(I'm trying to perform enemy1.getx() and enemy1.gety(), also gives me a Null pointer exception):
//bullet-smallenemy1 collision
for(int i = 0; i < bullet1.size(); i++){
Bullet1 b = bullet1.get(i);
for(int j = 0; j < smallenemy1.size(); j++){
SmallEnemy1 s = smallenemy1.get(j);
if(s.contains(b.getx(), b.gety())){
bullet1.remove(i);
i--;
smallenemy1.remove(j);
j--;
break;
}
}
}我已经在下面粘贴了所有内容,以防上面的内容不起作用。我知道有很多乱七八糟的东西。我需要解决的就是空指针异常。
http://pastebin.com/3CqkNTgd
发布于 2015-06-18 20:23:58
NullpointerException表示播放器根本没有实例化,或者设置为null。您似乎从未调用过init()方法。
https://stackoverflow.com/questions/30905310
复制相似问题