如何在int数组Java中使用/分配元素[重复]

问题描述 投票:0回答:1

我已经在代码顶部初始化了一个整数数组。我正在尝试使用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;
}
}
}
java arrays java.util.scanner
1个回答
0
投票

您忘了初始化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];
© www.soinside.com 2019 - 2024. All rights reserved.