我已经在代码顶部初始化了一个整数数组。我正在尝试使用for循环从该数组中检索默认值,并将其分配给默认值(可能为0),然后向其添加一个。但是,当我尝试使用方法readLines()时,它返回错误:
java.lang.NullPointerException
ScrabblePrecision类
public class ScrabblePrecision extends Echo{
int[] letterFreqPoints;
public void readLines(){
for(int i = 0; i < letters.length; i++){
char letter = letters[I];
char lower = Character.toLowerCase(letter);
int asciiValue= (int)lower;
int position= asciiValue-96;
letterFreqPoints[position - 1] = letterFreqPoints[position - 1] + 1;
}
}
}
您忘了初始化int数组。
// I am assuming the size should be 26 because it's letter count
// you can keep the size to any number you seem appropriate
int[] letterFreqPoints = new int[26];