Graph 的 addEdge 是 return 空指针?

addEdge for Graph is return null pointer?

所以我试图理解散列图和图表,当我 运行 我的代码时,我总是得到一个空指针异常。在第 25 行 "adjVertices.get(v1).add(v2);" 并且我相信如果它到达它,下一行也会抛出它。我错过了什么?提前致谢。

import java.util.*;

public class Graph {

    private Map<Vertex, List<Vertex>> adjVertices;

    Graph() {
        this.adjVertices = new HashMap<Vertex, List<Vertex>>();
    }

    class Vertex {
        String label;
        Vertex(String label) {
            this.label = label;
        }
    }

    void addVertex(String label) {
        adjVertices.putIfAbsent(new Vertex(label), new ArrayList<>());
    }

    void addEdge(String label1, String label2) {
        Vertex v1 = new Vertex(label1);
        Vertex v2 = new Vertex(label2);
        adjVertices.get(v1).add(v2);
        adjVertices.get(v2).add(v1);
    }

}

这是我的主图:

public class Main {

    public static void main(String args[]) {
        // Create a graph given in the above diagram
        Graph g = new Graph();
        g.addVertex("CS2010");
        g.addVertex("CS2370");
        g.addVertex("CS2381");
        g.addVertex("CS3221");
        g.addVertex("CS3600");
        g.addEdge("CS2010", "CS2370");
        g.addEdge("CS2370", "CS2381");
        g.addEdge("CS2370", "CS3600");
        g.addEdge("CS2381", "CS3221");

    }
}

I always get a null pointer exception. on line 25 with "adjVertices.get(v1).add(v2) ... what am I missing?

如果 adjVertices.get(v1) returns null,表达式 adjVertices.get(v1).add(v2) 将抛出 NullPointerException 因为 .add(v2) 将在 null参考。如下操作:

void addEdge(String label1, String label2) {
    Vertex v1 = new Vertex(label1);
    Vertex v2 = new Vertex(label2);
    if (adjVertices.get(v1) != null) {
        adjVertices.get(v1).add(v2);
    }
    if (adjVertices.get(v2) != null) {
        adjVertices.get(v2).add(v1);
    }
}

您的代码中的另一个问题是您没有在 Vertex 中实现 hashCodeequals。每当您要将此 class 的实例添加到 HashMap 中时,请确保在 class 中实现 hashCodeequals。完整代码如下:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

class Vertex {
    String label;

    Vertex(String label) {
        this.label = label;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(label);
    }

    @Override
    public boolean equals(Object obj) {
        return label.equals(((Vertex) obj).label);
    }
}

class Graph {

    public Map<Vertex, List<Vertex>> adjVertices;

    Graph() {
        this.adjVertices = new HashMap<Vertex, List<Vertex>>();
    }

    void addVertex(String label) {
        adjVertices.putIfAbsent(new Vertex(label), new ArrayList<>());
    }

    void addEdge(String label1, String label2) {
        Vertex v1 = new Vertex(label1);
        Vertex v2 = new Vertex(label2);
        if (adjVertices.get(v1) != null) {
            adjVertices.get(v1).add(v2);
        }
        if (adjVertices.get(v2) != null) {
            adjVertices.get(v2).add(v1);
        }
    }
}

public class Main {
    public static void main(String args[]) {
        // Create a graph given in the above diagram
        Graph g = new Graph();
        g.addVertex("CS2010");
        g.addVertex("CS2370");
        g.addVertex("CS2381");
        g.addVertex("CS3221");
        g.addVertex("CS3600");
        g.addEdge("CS2010", "CS2370");
        g.addEdge("CS2370", "CS2381");
        g.addEdge("CS2370", "CS3600");
        g.addEdge("CS2381", "CS3221");
        System.out.println(g.adjVertices);
    }
}

输出:

{Vertex@76fdc54d=[Vertex@76fdd14a], Vertex@76fe414e=[Vertex@76fdd16a], Vertex@76fdd14a=[Vertex@76fdc54d, Vertex@76fdd16a, Vertex@76fe5013], Vertex@76fdd16a=[Vertex@76fdd14a, Vertex@76fe414e], Vertex@76fe5013=[Vertex@76fdd14a]}