title: java.lang.NullPointerException when accessing a vector2 array
body: <p>i have 2 classes, AI.java and TestColl.java. </p>
  
  <p>TestColl.java has the same problem as AI.java</p>
  
  <p>the following block of codes are from AI.java.</p>
  
  <pre><code>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 &lt; 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));
      }
  }
  </code></pre>
  
  <p>in PlaceEnemy method most of my vector variables have been given values. BUT! when i access them, the vector2 variables, from another method they return null.</p>
  
  <p>for example. i'll call LogicChase() method to apply the AI for my Enemies. </p>
  
  <p>the following block of codes are still inside AI.java class.</p>
  
  <pre><code>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 &lt;= 50){
              chasing = true;
          }
  
          else if(Choose &gt; 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&lt;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 &lt;= 0){// if counter has yet to be set.
                      // SET DIRECTIONS FOR X AND Y;
                      for(int i = 0; i &lt; 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 &lt;= 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&lt;spawn.length; i++){
                          move(i);
                      }
                  }
              }
          }
      }       
  
      else if(chasing){
          for(int i = 0; i &lt; 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 &lt; spawn.length; i++){
              if(EnemPos[i].y &gt; EnemPos[i].x){
                  EnemVel[i].y = velocity/2;
                  EnemVel[i].x = velocity/3;
              }
  
              if(EnemPos[i].x &gt; 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 &gt; tc.position.x)
                  {EnemVel[i].x*=-1;}
              if (spawn[i].y &gt; tc.position.y)
                  {EnemVel[i].y*=-1;}
          }
          if(count &lt;= 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;
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>every time i try to access EnemPos[i].x or spawn[i].x from inside the LogicChase method i always get the "java.lang.NullPointerException" at AI.logicChase(AI.java:218) or AI.logicChase(AI.java:142)</p>
  
  <p>the code on AI.java:218 is</p>
  
  <pre><code>// **JAVA.LANG.NULL DETECTED AT THIS LINE OF CODE**
  EnemPos[i].x = Math.abs(spawn[i].x - tc.position.x);
  </code></pre>
  
  <p>the code on AI.java:142 is</p>
  
  <pre><code>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));
  }
  </code></pre>
  
  <p>my question is simple. WHY THE HELL IS IT NULL WHEN I ALREADY DECLARED VALUES FROM MY VECTOR2 VARIABLES?!</p>
  
  <p>also for those who want to check the AI.java and TestColl.java here are the links.
  <a href="http://www.mediafire.com/view/t6sz5ifsokch1av/AI.java" rel="nofollow">http://www.mediafire.com/view/t6sz5ifsokch1av/AI.java</a>
  <a href="http://www.mediafire.com/view/6ssvky2zpuksbuk/TestColl.java" rel="nofollow">http://www.mediafire.com/view/6ssvky2zpuksbuk/TestColl.java</a></p>
  
  <p>just replace the TestColl implements with application listener.</p>