私はnullPointerException
自分のプログラムで遭遇しましたが、使用されている変数ベクトルはインスタンス化され、メソッドにも設定されています。問題は、問題をその根本までたどろうとしたことですが、問題が null になる理由に対する答えがまだありません。
コード:
static class AI {
private String log = AI.class.getSimpleName();
private boolean lock = false , placed = false , moving = false ,
chasing = true, drop = false, decision = false;
private float counter, x, y;
private int xDir, yDir;
private DugMan d;
private Enemy e;
public void Checker() {
if (chasing) {
e.ePosition.x += Gdx.graphics.getDeltaTime()*x;
e.ePosition.y += Gdx.graphics.getDeltaTime()*y;
}
if (moving) {Gdx.app.log(log, Integer.toString(xDir, yDir));}
if (chasing) {Gdx.app.log(log, Float.toString(counter));}
if (counter > 0) {counter--;}
if (e.ePosition.y > Gdx.graphics.getHeight() - 10) {
e.ePosition.y = Gdx.graphics.getHeight() - 10;
}
if (e.ePosition.y <= 0) {
e.ePosition.y = 1;
}
if (e.ePosition.x > Gdx.graphics.getWidth() - 10) {
e.ePosition.x = Gdx.graphics.getWidth() - 10;
}
if (e.ePosition.x <= 0) {
e.ePosition.x = 1;
}
}
public int chooseRandomDirection() {
Random r = new Random();
int[] randDirection = new int[3];
randDirection[0] = 0;
randDirection[1] = 1;
randDirection[2] = -1;
int randChoice = r.nextInt(3);
return randDirection[randChoice];
}
public void setXDirection(int dir) {
xDir = dir;
}
public void setYDirection(int dir) {
yDir = dir;
}
public void move() {
e.ePosition.x += (Gdx.graphics.getDeltaTime()* (e.eMax_vel/2)) * xDir;
e.ePosition.y += (Gdx.graphics.getDeltaTime()* (e.eMax_vel/2)) * yDir;
}
public void Decider() {
if (!decision) {
Random rand = new Random();
int Choose = rand.nextInt(100);
if (Choose <= 50) {
chasing = true;
}
else if (Choose > 50) {
chasing = false;
}
decision = true;
}
}
public void logicChase() {
Decider();
if (!chasing) {// if not in chase mode.
if (!moving) {// if stationary.
if (!lock) {// if directions has not yet to be set.
if (counter <= 0) {// if counter has yet to be set.
// SET DIRECTIONS FOR X AND Y;
setXDirection(chooseRandomDirection());
setYDirection(chooseRandomDirection());
// SET LOCK TO TRUE
lock = true;
// SET MOVING TO TRUE
moving = true;
// SET TIME LENGTH OF ENEMY MOVEMENT
counter = 250;
}
}
}
else if (moving) {// if enemy is on the move.
if (counter <= 0) {// if time counter for enemy movement has finished.
if (lock) {// if directions are in used.
// SET MOVING TO FALSE. ENEMY IS NOW RESTING
moving = false;
// SET NEW TIME COUNTER FOR ENEMY WHILE AT REST
counter = 500;
// SET LOCKS TO FALSE. DIRECTIONS ARE NOT IN USE.
lock = false;
// RANDOMIZE ENEMY MOVEMENT
decision = false;
}
}
else {// ENEMY IS STILL MOVING, ENEMY IS NOW MOVING.
if (lock) {// IF DIRECTIONS ARE STILL LOCKED.
move();
}
}
}
}
else if (chasing) {
float xdif = Math.abs(e.ePosition.x - d.dPosition.x); <<2nd trace error
float ydif = Math.abs(e.ePosition.y - d.dPosition.y);
if (ydif > xdif ){
y = e.eMax_vel;
x = e.eMax_vel/2;
}
if(xdif > ydif) {
x = e.eMax_vel;
y = e.eMax_vel/2;
}
if (xdif == 0) {
x = e.eMax_vel;
y = 0;
}
if (ydif == 0) {
y = e.eMax_vel;
x = 0;
}
if (ydif == xdif) {
y = e.eMax_vel/2;
x = e.eMax_vel/2;
}
if (e.ePosition.x > d.dPosition.x)
{x*=-1;}
if (e.ePosition.y > d.dPosition.y)
{y*=-1;}
if (counter <= 0) {
if (chasing) {//if enemy is chasing
if (!lock) {//lock is to check if counter has been set.
counter = 500;
lock = true;
}
else {
lock = false;
chasing = false;
}
}
else if (!chasing) {//if enemy stopped chasing
if (!lock) {//lock is to check if counter has been set.
counter = 250;
lock = true;
decision = false;
}
else {
lock = false;
chasing = true;
}
}
}
}
}
}
ここにトレースがあります:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.teamkwan.dugmanalpha.level.TestLevel1$AI.logicChase(TestLevel1.java:552)
at com.teamkwan.dugmanalpha.level.TestLevel1.show(TestLevel1.java:908)
行 552 は次の行です。
float xdif = Math.abs(e.ePosition.x - d.dPosition.x);
また、eposition と dposition はどちらも参照用に公開されており、他の方法でもインスタンス化されています。