所以我写了这段代码来读取包含数字的文件,但是当我尝试为数组赋值时,我收到了NullPointerException错误。
这是我的代码:
private static int []a;
public static int i = 0;
public static void main(String[] args) {
// Get a random generated array
// I'll read from file, which contains generated list of numbers.
BufferedReader reader = null;
try {
File file = new File("numbers.txt");
reader = new BufferedReader(new FileReader(file));
for(String line = reader.readLine(); line!= null; line = reader.readLine()){
//I get error here
a[i] = Integer.parseInt(line);
i++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
因此,我编写此代码来读取包含数字的文件,但是当我尝试为数组赋值时,出现NullPointerException错误。这是我的代码:private static int [] a;公共...
您忘记了初始化数组