So I am trying to understand hashmaps and graphs, when I run my code, I always get a null pointer exception. on line 25 with "adjVertices.get(v1).add(v2);" and I am sure the next line would throw it too if it would get to it. what am I missing? Thanks in advance.
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);
}
}
Here is what my main looks like:
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?
If adjVertices.get(v1)
returns null
, the expression, adjVertices.get(v1).add(v2)
will throw NullPointerException
because .add(v2)
will be called on a null
reference. Do it as follows:
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);
}
}
Another problem in your code is that you haven't implemented hashCode
and equals
in Vertex
. Make sure to implement hashCode
and equals
in the class whenever you are going to add the instances of this class into a HashMap
. Given below is the complete code:
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);
}
}
Output:
{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]}