Skip to content

Commit 9d86ee7

Browse files
committed
fix: Resolve Infer null dereference warning in Dijkstra
1 parent aa780a9 commit 9d86ee7

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

  • src/main/java/com/thealgorithms/datastructures/graphs

src/main/java/com/thealgorithms/datastructures/graphs/Dijkstra.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,15 @@ public String toString() {
171171

172172
// another pass to set neighbouring vertices
173173
for (Edge e : edges) {
174-
graph.get(e.v1).neighbours.put(graph.get(e.v2), e.dist);
175-
// graph.get(e.v2).neighbours.put(graph.get(e.v1), e.dist); // also do this for an
176-
// undirected graph
174+
var v1 = graph.get(e.v1);
175+
var v2 = graph.get(e.v2);
176+
177+
// this null-check satisfies Infer static analyzer
178+
if (v1 != null && v2 != null) {
179+
v1.neighbours.put(v2, e.dist);
180+
// v2.neighbours.put(v1, e.dist); // also do this for an
181+
// undirected graph
182+
}
177183
}
178184
}
179185

0 commit comments

Comments
 (0)