I'm trying to implement Graph using adjacency list, but when I try to add new pair into the list it gaves me an NullPointerException. Is there anyone know why it would happen and how to fix it?
Thanks in advance.
My Graph.java is like:
import javafx.util.Pair;
import java.util.*;
public class Graph{
private final HashMap<String, List<Pair<String, Integer>>> adj;
public Graph(String filePath) {
adj = new HashMap<>();
addEdge("a", "b", 30);
}
public void addEdge(String start, String end, int weight) {
adj.get(start).add(new Pair<>(end, weight));
}
}
My Main.java is like:
public class Main {
public static void main(String[] args) {
Graph gr;
try {
gr = new Graph("test.txt");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}