这对我来说有点奇怪。我创建了一个静态的Integer数组,然后我尝试为它分配一个值。但是我在degree[i] = 0
行得到了一个Null指针异常。
由于我在分配之前没有读取该值,因此我不明白为什么会出现NullPointer异常。
private static Integer[] degree;
public static void initDegree(int num_of_vertices) throws Exception{
for (int i = 0; i < num_of_vertices; i++) {
degree[i] = 0;
}
}
答案 0 :(得分:3)
您需要初始化数组。
离
degree = new Integer[5];
否则,数组本身只是一个空值。