Java array NullPointerException

2019-09-09 08:46发布

问题:

The code block is listed below:

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

This is the Vertex class:

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

Actually, I just want to copy the element in AdjList to a new array subgraph. But the error message shows that "java.lang.NullPointerException" and shows the problem lies in "subGraph[i].nodeDelay = AdjList[i].nodeDelay;" line.

I tested by printing to the console. And found the AdjList.length is 8 and the problem occurs in the very first round; And even when I only write "subGraph[i].nodeDelay;" without assigning any value to it, it also shows the wrong message. Any idea on this? Thanks in advance.

回答1:

Add it inside the loop:

subGraph[i] = new Vertex();

You first need to instantiate an object (subGraph[i] in your case) before accessing it.



回答2:

The answer is: uninitialized variable. You did initialize subGraph to be an array, but you did not initialize subGraph[i].



回答3:

When you create an object array in java, it's automatically initialized with null values. It's your responsibility to loop on the array an populate it with references to new objects. In your case, you should assign Vertex objects to all array positions.



回答4:

You need to create a Vertex instance and place it in the array prior to setting any fields.

Something like:

public static Vertex[] computeSubGraph(Vertex[] AdjList, int[] retiming)
{
    Vertex[] subGraph = new Vertex[AdjList.length];
    for (int i = 0; i < AdjList.length; i++) {

        subGraph[i] = new Vertex(); // adding instance prior to setting fields.

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


回答5:

On add

subGraph[i] = new Vertex();

before this line

subGraph[i].nodeDelay = AdjList[i].nodeDelay;


回答6:

Because subGraph[i] is initially null . So subGraph[i].nodeDelay will obviously throw a NullPointerException, because you are trying to access or modify the field of a null object. Array of references will always be initialized with elements of null references by default.