gpt4 book ai didi

Java 数组 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 07:04:14 24 4
gpt4 key购买 nike

代码块如下:

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;
}

这是顶点类:

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中的元素复制到一个新的数组subgraph中。但错误消息显示“java.lang.NullPointerException”并显示问题出在“subGraph[i].nodeDelay = AdjList[i].nodeDelay;”线。

我通过打印到控制台进行测试。发现AdjList.length为8,问题出现在第一轮;即使我只写“subGraph[i].nodeDelay;”如果不给它分配任何值,它也会显示错误的消息。对此有什么想法吗?提前致谢。

最佳答案

将其添加到循环中:

subGraph[i] = new Vertex();

在访问它之前,您首先需要实例化一个对象(在您的情况下为 subGraph[i])。

关于Java 数组 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16295641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com