0

I have the following code from the book Data Structures & and Algorithms - Goodrich:

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 + ")";
}
}

Scores Class:

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 & 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 < numEntries; i++) {
        if (i > 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 <= 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 >= 1) && (newScore > 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
}
}

Test Class:

 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());


}

}

In the method add(GameEntry entry) I changed the forloop as follows:

for (; (i >= 1); i--) {
    if (newScore > entries[i - 1].getScore()) {
            entries[i] = entries[i - 1];      // move entry i one to the right
    }
}

I am getting a NullPointerException in the if statement. Why is not coming in the forloop?

HadiRj
  • 1,015
  • 3
  • 21
  • 41
Phillip
  • 87
  • 1
  • 12

1 Answers1

1

NullPointerException is thrown when an application attempts to use an object reference, having the null value.

In your case, this call happens in if statement:

entries[i - 1].getScore() // entries[i - 1] is null

In your for statement you do not call any method on any object reference, so there is no reason for NullPointerException to be raised there.

Yury Fedorov
  • 14,508
  • 6
  • 50
  • 66