1

nullPointerException在我的程序中遇到过,但是正在使用的变量向量被实例化并且也在方法中设置。问题是我们试图追踪问题的根源,但仍然没有回答我们的问题为什么它会为空。

代码:

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 都是公开的以供参考,并且它们也在其他方法中实例化。

4

2 回答 2

0

您需要逐步调试调试器,并确保创建de正在调用的方法在使用d和的方法之前被调用e。某些事情并没有按照您认为的方式执行。

这里的关键是使用调试器,这样您就可以看到发生的事情。不要依赖阅读代码并尝试在脑海中执行。

于 2013-10-03T18:36:48.017 回答
0

仔细检查您是否正在初始化变量eandd和.ePositiondPosition

于 2013-10-03T18:14:46.507 回答