Java 图形的addEdge是否返回空指针?
所以我试图理解hashmaps和图,当我运行代码时,我总是得到一个空指针异常。在第25行用“adjVertices.get(v1).add(v2);”我确信下一行如果能找到它也会抛出它。我错过了什么?提前谢谢Java 图形的addEdge是否返回空指针?,java,graph,nullpointerexception,vertex,adjacency-list,Java,Graph,Nullpointerexception,Vertex,Adjacency List,所以我试图理解hashmaps和图,当我运行代码时,我总是得到一个空指针异常。在第25行用“adjVertices.get(v1).add(v2);”我确信下一行如果能找到它也会抛出它。我错过了什么?提前谢谢 import java.util.*; public class Graph { private Map<Vertex, List<Vertex>> adjVertices; Graph() { this.adjVertices
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);
}
}
我总是得到一个空指针异常。第25行
“adjVertices.get(v1).add(v2)…我缺少什么
如果adjVertices.get(v1)
返回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
中实现hashCode
和equals
。在将此类实例添加到HashMap
时,请确保在类中实现hashCode
和equals
。下面给出了完整的代码:
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);
}
}
我总是得到一个空指针异常
“adjVertices.get(v1).add(v2)。。。我错过了什么
如果adjVertices.get(v1)
返回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
中实现hashCode
和equals
。每当您要将此类的实例添加到HashMap
中时,请确保在类中实现hashCode
和equals
。以下是完整的代码:
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);
}
}
谢谢你的快速回复。我尝试了我们的your代码,但我的图形无法填充。我想他们总是空出来,我不知道你为什么最受欢迎。我只举了一个例子,我想你会把它应用于两个案例。在这两种情况下,即将
v1
添加到v2
或将v2
添加到v1
时,都需要选中null
。我已经相应地更新了我的答案。如果您面临任何进一步的问题,请告诉我。@user12763568-再次检查更新。我已经发布了一些重要的观点。我还发布了工作代码。如果您面临任何进一步的问题,请告诉我。谢谢您的快速回复。我尝试了我们的your代码,但我的图形无法填充。我想他们总是空出来,我不知道你为什么最受欢迎。我只举了一个例子,我想你会把它应用于两个案例。在这两种情况下,即将v1
添加到v2
或将v2
添加到v1
时,都需要选中null
。我已经相应地更新了我的答案。如果您面临任何进一步的问题,请告诉我。@user12763568-再次检查更新。我已经发布了一些重要的观点。我还发布了工作代码。如果您有任何进一步的问题,请告诉我。