title: difference nullpointer between if and forloop
body: <p>I have the following code from the book Data Structures &amp; and Algorithms - Goodrich: </p>
  
  <pre><code>public class GameEntry {
  protected String name;  // name of the person earning this score
  protected int score;    // the score value
  
  public GameEntry(String n, int s) {
      name = n;
      score = s;
  }
  
  /**
   * Retrieves the name field
   */
  public String getName() {
      return name;
  }
  
  /**
   * Retrieves the score field
   */
  public int getScore() {
      return score;
  }
  
  /**
   * Returns a string representation of this entry
   */
  public String toString() {
      return "(" + name + ", " + score + ")";
  }
  }
  </code></pre>
  
  <p>Scores Class:</p>
  
  <pre><code>public class Scores {
  
  public static final int maxEntries = 10; // number of high scores we keep
  protected int numEntries;          // number of actual entries
  protected GameEntry[] entries;     // array of game entries (names &amp; scores)
  
  /**
   * Default constructor
   */
  public Scores() {
      entries = new GameEntry[maxEntries];
      numEntries = 0;
  }
  
  /**
   * Returns a string representation of the high scores list
   */
  public String toString() {
      String s = "[";
      for (int i = 0; i &lt; numEntries; i++) {
          if (i &gt; 0) {
              s += ", "; // separate entries by commas
          }
          s += entries[i];
      }
      return s + "]";
  }
  
  /**
   * Attempt to add a new score to the collection (if it is high enough)
   */
  public void add(GameEntry entry) {
      int newScore = entry.getScore();
      // is the new entry e really a high score?
      if (numEntries == maxEntries) { // the array is full
          if (newScore &lt;= entries[numEntries - 1].getScore()) {
              return;  // the new entry, e, is not a high score in this case
          }
      } else // the array is not full
      {
          this.numEntries++;
      }
      // Locate the place that the new (high score) entry e belongs
      int i = numEntries - 1; //first part outside the forloop because of the scope
      for (; (i &gt;= 1) &amp;&amp; (newScore &gt; entries[i - 1].getScore()); i--) { 
          entries[i] = entries[i - 1];      // move entry i one to the right
      }
      entries[i] = entry;               // add the new score to entries
  }
  }
  </code></pre>
  
  <p>Test Class:</p>
  
  <pre><code> public class TestGameEntry {
  
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
      Scores scores = new Scores();
      scores.add(new GameEntry("Phillip", 400));
      System.out.println(scores.toString());
      scores.add(new GameEntry("John", 200));
      System.out.println(scores.toString());
      scores.add(new GameEntry("Alice", 300));
      System.out.println(scores.toString());
      scores.add(new GameEntry("Paula", 500));
      System.out.println(scores.toString());
      scores.add(new GameEntry("Bob", 700));
      System.out.println(scores.toString());
      scores.add(new GameEntry("Charlie", 400));
      System.out.println(scores.toString());
      scores.add(new GameEntry("Addison", 1100));
      System.out.println(scores.toString());
      scores.add(new GameEntry("Sabina", 1200));
      System.out.println(scores.toString());
      scores.add(new GameEntry("Xenia", 300));
      System.out.println(scores.toString());
      scores.add(new GameEntry("Pablo", 300));
      System.out.println(scores.toString());
      scores.add(new GameEntry("Kadir", 300));
      System.out.println(scores.toString());
      scores.add(new GameEntry("Brigit", 1300));
      System.out.println(scores.toString());
      scores.add(new GameEntry("Brigit", 1500));
      System.out.println(scores.toString());
  
  
  }
  
  }
  </code></pre>
  
  <p>In the method <code>add(GameEntry entry)</code> I changed the forloop as follows:</p>
  
  <pre><code>for (; (i &gt;= 1); i--) {
      if (newScore &gt; entries[i - 1].getScore()) {
              entries[i] = entries[i - 1];      // move entry i one to the right
      }
  }
  </code></pre>
  
  <p>I am getting a <code>NullPointerException</code> in the if statement. Why is not coming in the forloop?</p>