LIBGDX:NPE错误跟踪和实例化

时间:2013-10-03 18:10:58

标签: java android libgdx

我在我的程序中遇到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都公开用于引用,并且它们也在其他方法中实例化。

2 个答案:

答案 0 :(得分:0)

仔细检查您是否正在初始化变量edePositiondPosition

答案 1 :(得分:0)

您需要逐步调试调试器,并确保在使用de的方法之前调用创建de的方法。有些事情没有按你认为的那样执行。

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