[英]Java Graph Implementation: nullpointerexception
I am trying to implement a graph in Java using an arrayList
of arrayList
. 我正在尝试使用
arrayList
的arrayList
在Java中实现图形。
I keep getting a NullPointerException
whenever the addEdge
function is called. 每当调用
addEdge
函数时,我都会不断收到NullPointerException
。 I cannot seem to figure out why. 我似乎不知道为什么。
Here is my code: 这是我的代码:
import java.util.ArrayList;
public class Graph {
private static ArrayList<ArrayList<Integer>> adjList;
public Graph(int vertices){
ArrayList<ArrayList<Integer>> adjList = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < vertices; i++){
adjList.add(new ArrayList<Integer>());
}
}
public void addEdge(int source, int destination){
adjList.get(source).add(destination);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
System.out.println("Neighbors of vertex 0: " + adjList.get(0));
System.out.println("Neighbors of vertex 2: " + adjList.get(2));
}
}
Please kindly advise. 请告知。
In your Graph
constructor, you're not initializing the static
member adjList
, but rather are defining a local one with the same name. 在
Graph
构造函数中,您不是要初始化static
成员adjList
,而是要定义一个具有相同名称的本地成员。 Also, there's no need adjList
to be static
, because it will be shared among all instances of Graph
. 另外,不需要
adjList
是static
,因为它将在Graph
所有实例之间共享。
Adjust it to: 将其调整为:
private ArrayList<ArrayList<Integer>> adjList;
public Graph(int vertices){
adjList = new ArrayList<ArrayList<Integer>>();
...
}
You should remove static
in adjList
field declaration. 您应该在
adjList
字段声明中删除static
。
This modifier makes adjList
a static instance which refers to null
. 此修饰符使
adjList
成为引用null
的静态实例。 And in the constructor you instantiate value for another adjList
that is local for your constructor (and is subject to be gathered by GC after the constructor is called). 并且在构造函数中,您实例化了另一个
adjList
值,该adjList
对于您的构造函数是本地的(并且在构造函数被调用后,有待GC收集)。 It is two completely different variables with the same name 这是两个完全不同的变量,具有相同的名称
change your constructor so that you are not declaring a local adjList variable 更改构造函数,以便不声明局部adjList变量
public Graph(int vertices){
adjList = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < vertices; i++){
adjList.add(new ArrayList<Integer>());
}
}
Also make this adjList variable non static since you want the adjList to be unique to each graph and not shared across all of them 还要使此adjList变量为非静态,因为您希望adjList对于每个图都是唯一的,并且不要在所有图之间共享
private ArrayList<ArrayList<Integer>> adjList;
In the Graph constructor, you declare an adjList variable, which is in conflict with your static class. 在Graph构造函数中,您声明adjList变量,该变量与您的静态类冲突。 You should replace
你应该更换
public Graph(int vertices){
ArrayList<ArrayList<Integer>> adjList = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < vertices; i++){
adjList.add(new ArrayList<Integer>());
}
}
by 通过
public Graph(int vertices){
adjList = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < vertices; i++){
adjList.add(new ArrayList<Integer>());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.