2013-10-03 2 views
1

В моей программе встречался nullPointerException, но используемый вектор-переменная создается и также устанавливается в методе. проблема в том, что мы попытались проследить проблему до ее корня и до сих пор не ответили на нашу проблему, почему она получает нуль.LIBGDX: трассировка ошибок и создание экземпляра NPE

Код:

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 находятся в общественных местах для ссылки и они также создаются другими способами.

ответ

0

Двойная проверка того, что вы инициализируете переменные e и d, ePosition и dPosition.

0

Вы должны пройти через отладчик и убедитесь, что методы, которые создают d и e в настоящее время вызывается перед методами, которые используют d и e. Что-то не выполняется так, как вы думаете.

Ключ здесь - использовать отладчик, чтобы вы могли видеть вещи по мере их возникновения. Не полагайтесь на чтение кода и попытку выполнить в своей голове.

Смежные вопросы