我有以下代码来自Data Structures&和算法 - 古德里奇:
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 + ")";
}
}
分数等级:
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
}
}
测试类:
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());
}
}
在方法add(GameEntry entry)
中,我按如下方式更改了forloop:
for (; (i >= 1); i--) {
if (newScore > entries[i - 1].getScore()) {
entries[i] = entries[i - 1]; // move entry i one to the right
}
}
我在if语句中得到NullPointerException
。为什么不进入forloop?
答案 0 :(得分:1)
当应用程序尝试使用具有null值的对象引用时,抛出
NullPointerException
。
在您的情况下,此调用发生在if
语句中:
entries[i - 1].getScore() // entries[i - 1] is null
在for
语句中,您不会在任何对象引用上调用任何方法,因此没有理由在那里引发NullPointerException
。