下面列出了代码块:
public static Vertex[] computeSubGraph(Vertex[] AdjList, int[] retiming)
{
Vertex[] subGraph = new Vertex[AdjList.length];
for (int i = 0; i < AdjList.length; i++) {
System.out.println(i);
subGraph[i].nodeDelay = AdjList[i].nodeDelay;
subGraph[i].predecessor = AdjList[i].predecessor;
subGraph[i].mark = AdjList[i].mark;
subGraph[i].starTime = AdjList[i].starTime;
subGraph[i].finishTime = AdjList[i].finishTime;
for (int j = 0; j < AdjList[i].inArcList.size(); j++) {
ArcNode old = AdjList[i].inArcList.get(j);
ArcNode newNode = new ArcNode(old.adjVex, old.arcWeight);
subGraph[i].outArcList.add(newNode);
subGraph[old.adjVex].inArcList.add(newNode);
}
}
return subGraph;
}
这是Vertex类:
public class Vertex implements Comparable<Vertex> {
public int arcWeight;
public int preDelay;
public boolean infinite = true;
public int nodeDelay = 0;
public Vertex predecessor = null;
public ArcNode firstArc = null;
public int mark = 0;
public int starTime;
public int finishTime;
public ArrayList<ArcNode> inArcList = new ArrayList<ArcNode>();
public ArrayList<ArcNode> outArcList = new ArrayList<ArcNode>();
}
实际上,我只想将AdjList中的元素复制到新的数组子图。但是错误消息显示“ java.lang.NullPointerException”并显示问题出在“ subGraph [i] .nodeDelay = AdjList [i] .nodeDelay;”。线。
我通过打印到控制台进行了测试。并发现AdjList.length为8并且问题发生在第一轮;甚至当我只写“ subGraph [i] .nodeDelay;”时没有为其分配任何值,它还会显示错误消息。
有什么想法吗?提前致谢。
最佳答案
将其添加到循环中:
subGraph[i] = new Vertex();
您首先需要实例化一个对象(在您的情况下为
subGraph[i]
),然后才能对其进行实例化。