全局字段的实例化
Instantiation of Global fields
我写了一个检测图中循环的代码。
给定输入,每行可以有几行 a # b .
表示从a到b有一条边。
我的代码工作了,然后我玩了一下,发现 a 行和 c 行以及 b 行和 d 行.
当我组合 a 行和 c 行并将声明和实例化一起写为 "ArrayList[]" arr=new ArrayList[99999] 时,b 行和 d 行类似,然后一切正常(假设我使适当的更改,例如传递 arr 和 visited 作为 DFS 和 DFSvisit 之间的参数)。
但是如果我像下面写的那样写它,那么我会得到空指针异常。这是什么原因?
class Test {
ArrayList<Integer>[] arr; // line a
boolean[] visited; // line b
public static void main (String[] args) throws java.lang.Exception {
Test obj=new Test();
obj.DFS();
}
public void DFS() {
arr=new ArrayList[99999]; // line c
visited=new boolean[99999]; // line d
for(int i=0;i<99999;i++) {
arr[i]=new ArrayList<>(); //instantiate each element of "array of Arraylists"
}
boolean[] flag=new boolean[99999]; //a flag array to identify which elements of array are valid elements
Scanner sc=new Scanner(System.in);
while(sc.hasNext()) {
int a=sc.nextInt(); //take first number
sc.next(); //throw away "#" token
int b=sc.nextInt(); //take second number
flag[a]=true; //means arr[a] is valid
arr[a].add(b); //add b to adjaceny list of arr[a]
}
Test obj=new Test();
for(int i=0;i<99999 ;i++) {
if (flag[i]) { //call DFSvisit only if arr[i] is valid
obj.DFSvisit(i);
}
}
System.out.println("Cycle does not exist");
}
void DFSvisit(int i) { // basic DFS implementation
visited[i] = true;
for (int j=0;j<arr[i].size();) {
if (visited[arr[i].get(j)]==true) {
System.out.println("Cycle exists");
System.exit(0);
} else {
DFSvisit(arr[i].get(j));
}
j++;
}
visited[i] = false;
}
}
错误:
Exception in thread "main" java.lang.NullPointerException
at test.Test.DFSvisit(Test.java:59)
at test.Test.DFS(Test.java:49)
at test.Test.main(Test.java:18)
P.S。我知道如果我在初始化之前尝试使用引用变量,那么我将得到 Null Pointer Exception。但是在这里我看不到发生这种情况的任何地方。我已经宣布 arr 和 visited 作为成员class(在每个方法之外)。然后我在从 main 方法移动到 DFS[=49 之后初始化它们=] 方法。在这两者之间,我没有尝试使用 arr 或 visited 但我还是得到了 NPE。这是为什么?
你只是混淆了两个对象。
class Test {
ArrayList<Integer>[] arr;
boolean[] visited;
public static void main (String[] args) throws java.lang.Exception {
Test obj=new Test(); // here you create the first object. arr is null here
obj.DFS();
}
public void DFS() {
arr=new ArrayList[99999];
visited=new boolean[99999]; // here you initialize the fields of the first instance of the object
...
Test obj=new Test(); // here you create the second instance of the object. arr is null
for(int i=0;i<99999 ;i++) {
if (flag[i]) {
obj.DFSvisit(i); // here you call the method of the second object where arr is null. NPE
}
}
如果合并这些行,那么字段初始化将在对象构造期间发生。这就是为什么没有 NPE 的原因。
我写了一个检测图中循环的代码。
给定输入,每行可以有几行 a # b .
表示从a到b有一条边。
我的代码工作了,然后我玩了一下,发现 a 行和 c 行以及 b 行和 d 行.
当我组合 a 行和 c 行并将声明和实例化一起写为 "ArrayList[]" arr=new ArrayList[99999] 时,b 行和 d 行类似,然后一切正常(假设我使适当的更改,例如传递 arr 和 visited 作为 DFS 和 DFSvisit 之间的参数)。
但是如果我像下面写的那样写它,那么我会得到空指针异常。这是什么原因?
class Test {
ArrayList<Integer>[] arr; // line a
boolean[] visited; // line b
public static void main (String[] args) throws java.lang.Exception {
Test obj=new Test();
obj.DFS();
}
public void DFS() {
arr=new ArrayList[99999]; // line c
visited=new boolean[99999]; // line d
for(int i=0;i<99999;i++) {
arr[i]=new ArrayList<>(); //instantiate each element of "array of Arraylists"
}
boolean[] flag=new boolean[99999]; //a flag array to identify which elements of array are valid elements
Scanner sc=new Scanner(System.in);
while(sc.hasNext()) {
int a=sc.nextInt(); //take first number
sc.next(); //throw away "#" token
int b=sc.nextInt(); //take second number
flag[a]=true; //means arr[a] is valid
arr[a].add(b); //add b to adjaceny list of arr[a]
}
Test obj=new Test();
for(int i=0;i<99999 ;i++) {
if (flag[i]) { //call DFSvisit only if arr[i] is valid
obj.DFSvisit(i);
}
}
System.out.println("Cycle does not exist");
}
void DFSvisit(int i) { // basic DFS implementation
visited[i] = true;
for (int j=0;j<arr[i].size();) {
if (visited[arr[i].get(j)]==true) {
System.out.println("Cycle exists");
System.exit(0);
} else {
DFSvisit(arr[i].get(j));
}
j++;
}
visited[i] = false;
}
}
错误:
Exception in thread "main" java.lang.NullPointerException
at test.Test.DFSvisit(Test.java:59)
at test.Test.DFS(Test.java:49)
at test.Test.main(Test.java:18)
P.S。我知道如果我在初始化之前尝试使用引用变量,那么我将得到 Null Pointer Exception。但是在这里我看不到发生这种情况的任何地方。我已经宣布 arr 和 visited 作为成员class(在每个方法之外)。然后我在从 main 方法移动到 DFS[=49 之后初始化它们=] 方法。在这两者之间,我没有尝试使用 arr 或 visited 但我还是得到了 NPE。这是为什么?
你只是混淆了两个对象。
class Test {
ArrayList<Integer>[] arr;
boolean[] visited;
public static void main (String[] args) throws java.lang.Exception {
Test obj=new Test(); // here you create the first object. arr is null here
obj.DFS();
}
public void DFS() {
arr=new ArrayList[99999];
visited=new boolean[99999]; // here you initialize the fields of the first instance of the object
...
Test obj=new Test(); // here you create the second instance of the object. arr is null
for(int i=0;i<99999 ;i++) {
if (flag[i]) {
obj.DFSvisit(i); // here you call the method of the second object where arr is null. NPE
}
}
如果合并这些行,那么字段初始化将在对象构造期间发生。这就是为什么没有 NPE 的原因。