public void createGraph () {
int oldFrom = -1;
int oldTo = -1;
for(int i = 0; i < edges.size(); i++) {
EdgeI e = edges.get(i);
int from = e.from;
int to = e.to;
VertexI v = vertices.get(from);
if (from == oldFrom && to == oldTo){
vertexWeight wic = v.neighbors.get(v.neighbors.size() - 1);
wic.w++;
}
else {
v.neighbors.add(new vertexWeight (to, 1));
oldFrom = from;
oldTo = to;
}
}
}
neighbors
是List
来自VertexI
班级的公众。w
是vertexWeight
类的公共整数。edges
是位于我的主要课程中的列表。我不断收到这行代码的空指针异常:
v.neighbors.add(new vertexWeight (to, 1));
尝试了大约 15 分钟,但我没有让它工作。我在搞什么鬼?
java.lang.NullPointerException
at tester.createGraph(tester.java:60)
at tester.main(tester.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)