访问vector2数组时的java.lang.NullPointerException

时间:2013-10-03 01:24:46

标签: java

我有两个类,AI.java和TestColl.java。

TestColl.java与AI.java具有相同的问题

以下代码块来自AI.java。

public Vector2[] spawn = new Vector2[2];
public Vector2[] EnemPos = new Vector2[spawn.length];
public Vector2[] EnemVel = new Vector2[spawn.length];

 public void PlaceEnemy(){
    for(int i = 0; i < spawn.length;i++)//places enemies for the first time.
    {
        EnemPos[i] = new Vector2(0, 0);
        EnemVel[i] = new Vector2(0, 0);
        Gdx.app.log(log, "Creating EnemPos[" + Integer.toString(i) +"].x = " + Float.toString(EnemPos[i].x));
        Gdx.app.log(log, "Creating EnemPos[" + Integer.toString(i) +"].y = " + Float.toString(EnemPos[i].y));
        Gdx.app.log(log, "Creating EnemVel[" + Integer.toString(i) +"].x = " + Float.toString(EnemVel[i].x));
        Gdx.app.log(log, "Creating EnemVel[" + Integer.toString(i) +"].y = " + Float.toString(EnemVel[i].y));
        x = MathUtils.random(10,50);
        y = MathUtils.random(10,50);
        spawn[i] = new Vector2(x, y);
        Gdx.app.log(log, "spawn["+ i + "], " + Float.toString(spawn[i].x) + ", " + Float.toString(spawn[i].y));
    }
}

在PlaceEnemy方法中,我的大多数矢量变量都被赋予了值。但!当我访问它们时,vector2变量,从另一个方法返回null。

例如,

。我将调用LogicChase()方法将AI应用于我的敌人。

以下代码块仍在AI.java类中。

public void Decider(){
    if(!decision){
        // reason why it's 50/50 is that 
        // we will lower or raise the chance of having chasing = true
        // when the time comes but for now it's only at 50/50
        Random rand = new Random();
        int Choose = rand.nextInt(100);

        if(Choose <= 50){
            chasing = true;
        }

        else if(Choose > 50){
            chasing = false;
        }
        decision = true;
    }
}

public float chooseRandomDirection(){
    Random r = new Random();
    float[] randDirection = new float[3];
    randDirection[0] = 0;
    randDirection[1] = 1;
    randDirection[2] = -1;
    int randChoice = r.nextInt(3);
    return randDirection[randChoice];
}

public void setXDirection(float dir, int i){
    EnemVel[i].x = 0;
    Gdx.app.log(log, "Old EnemVel[" + Integer.toString(i) +"].x = " + Float.toString(EnemVel[i].x));
    EnemVel[i].x = dir;
    Gdx.app.log(log, "New EnemVel[" + Integer.toString(i) +"].x = " + Float.toString(EnemVel[i].x));
}

public void setYDirection(float dir, int i){
    EnemVel[i].y = 0;
    Gdx.app.log(log, "Old EnemVel[" + Integer.toString(i) +"].y = " + Float.toString(EnemVel[i].y));
    EnemVel[i].y = dir;
    Gdx.app.log(log, "New EnemVel[" + Integer.toString(i) +"].y = " + Float.toString(EnemVel[i].y));
}

public void move(int i){
    spawn[i].x += (Gdx.graphics.getDeltaTime()* (velocity/2)) * EnemVel[i].x;
    spawn[i].y += (Gdx.graphics.getDeltaTime()* (velocity/2)) * EnemVel[i].y;
}

public void logicChase() 
{
    for(int i=0; i<spawn.length; i++){
        Gdx.app.log(log, "Old EnemVel[" + Integer.toString(i) +"].x = " + Float.toString(EnemVel[i].x));
        Gdx.app.log(log, "Old EnemVel[" + Integer.toString(i) +"].y = " + Float.toString(EnemVel[i].y));
        Gdx.app.log(log, "Old EnemPos[" + Integer.toString(i) +"].x = " + Float.toString(EnemPos[i].x));
        Gdx.app.log(log, "Old EnemPos[" + Integer.toString(i) +"].y = " + Float.toString(EnemPos[i].y));
    }
    Decider();
    if(!chasing){// if not in chase mode.
        if(!moving){// if stationary.
            if(!lock){// if directions has not yet to be set.
                if(count <= 0){// if counter has yet to be set.
                    // SET DIRECTIONS FOR X AND Y;
                    for(int i = 0; i < spawn.length; i++){
                        EnemVel[i].y = 0;
                        Gdx.app.log(log, "Setting new EnemVel["+i+"] x and y");
                        setXDirection(chooseRandomDirection(),i);
                        setYDirection(chooseRandomDirection(),i);
                        Gdx.app.log(log, "New EnemVel["+i+"] x and y set");
                    }
                    // SET LOCK TO TRUE
                    lock = true;
                    // SET MOVING TO TRUE
                    moving = true;
                    // SET TIME LENGTH OF ENEMY MOVEMENT
                    count = 250;
                }
            }
        }
        else if(moving){// if enemy is on the move.
            if(count <= 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
                    count = 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.
                    for(int i=0; i<spawn.length; i++){
                        move(i);
                    }
                }
            }
        }
    }       

    else if(chasing){
        for(int i = 0; i < spawn.length; i++){

            //can't access EnemPos[i].x;
            EnemPos[i].x = 0;
            EnemPos[i].y = 0;

            Gdx.app.log(log, "Setting new EnemPos[" + i + "] x and y");
            Gdx.app.log(log, "Old EnemPos[" + Integer.toString(i) +"].x = " + Float.toString(EnemPos[i].x));
            EnemPos[i].x = Math.abs(spawn[i].x - tc.position.x);
            Gdx.app.log(log, "New EnemPos[" + Integer.toString(i) +"].x = " + Float.toString(EnemPos[i].x));

            Gdx.app.log(log, "Old EnemPos[" + Integer.toString(i) +"].y = " + Float.toString(EnemPos[i].y));
            EnemPos[i].y = Math.abs(spawn[i].y - tc.position.y);
            Gdx.app.log(log, "New EnemPos[" + Integer.toString(i) +"].y = " + Float.toString(EnemPos[i].y));
        }
        for(int i = 0; i < spawn.length; i++){
            if(EnemPos[i].y > EnemPos[i].x){
                EnemVel[i].y = velocity/2;
                EnemVel[i].x = velocity/3;
            }

            if(EnemPos[i].x > EnemPos[i].y){
                EnemVel[i].x = velocity/2;
                EnemVel[i].y = velocity/3;
            }
            if(EnemPos[i].x == 0){
                EnemVel[i].x = velocity;
                EnemVel[i].y = 0;
            }
            if(EnemPos[i].y == 0){
                EnemVel[i].x = velocity;
                EnemVel[i].x = 0;
            }
            if(EnemPos[i].y == EnemPos[i].x){
                EnemVel[i].y = velocity/3;
                EnemVel[i].x = velocity/3;
            }

            if (spawn[i].x > tc.position.x)
                {EnemVel[i].x*=-1;}
            if (spawn[i].y > tc.position.y)
                {EnemVel[i].y*=-1;}
        }
        if(count <= 0){
            if (chasing){//if enemy is chasing
                if(!lock){//lock is to check if counter has been set.
                    count = 200;
                    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.
                    count = 300;
                    lock = true;
                    decision = false;
                }
                else{
                    lock = false;
                    chasing = true;
                }
            }
        }
    }
}

每当我尝试从LogicChase方法中访问EnemPos [i] .x或spawn [i] .x时,我总是在AI.logicChase(AI.java:218)获取“java.lang.NullPointerException”或者AI.logicChase(AI.java:142)

AI.java:218上的代码是

// **JAVA.LANG.NULL DETECTED AT THIS LINE OF CODE**
EnemPos[i].x = Math.abs(spawn[i].x - tc.position.x);

AI.java:142上的代码是

public void setXDirection(int dir, int i){
    Gdx.app.log(log, "Old EnemPos[" + Integer.toString(i) +"].x = " + Float.toString(EnemPos[i].x));
    EnemVel[i].x = dir;// **JAVA.LANG.NULL DETECTED AT THIS LINE OF CODE**
    Gdx.app.log(log, "New EnemPos[" + Integer.toString(i) +"].x = " + Float.toString(EnemPos[i].x));
}

我的问题很简单。为什么当我从我的VECTOR2变量中宣布声明值时,它是否为空?!

对于那些想要检查AI.java和TestColl.java的人来说,这里也是链接。 http://www.mediafire.com/view/t6sz5ifsokch1av/AI.java http://www.mediafire.com/view/6ssvky2zpuksbuk/TestColl.java

只需用应用程序监听器替换TestColl工具。

2 个答案:

答案 0 :(得分:0)

您尚未在数组EnemPos中放置元素。尝试删除

行中的评论
EnemPos[i] = new Vector2(x, y);

答案 1 :(得分:0)

您发布的代码均已注释。即:我无法看到方法setXDirection被调用的位置(全部被注释掉),但我想你调用的方法是之前,你调用public void PlaceEnemy(),你在哪里初始化数组中的向量。 请仔细检查一下。 (如果你可以发布非常棒的正确代码)