You're getting the NullPointerException
because the edges
field on your Vertex
object isn't getting properly initialized. It's generally best to use private
fields and getters; this would have caused warnings about the potential problem.
Inside your Vertex
class, you should initialize edges
. Since you haven't posted the code, we don't know what type it is, but if it's a Set
, for example, you would say:
Set<Edge> edges = Collections.emptySet(); // if you are going to replace edges
Set<Edge> edges = new HashSet<>(); // if you are going to use edges.add()
Edit: edges
is an array. The same principle applies; you aren't setting that edges
variable anywhere, and so it defaults to null
. A default that will prevent the immediate problem is
Edge[] edges = new Edge[0];
but you're better off refactoring to a collection type that will let you add an arbitrary number of edges, and better off still refactoring to enforce field encapsulation among your different classes.
Edit 2: The specific problem is with K
(DC) and M
(NY). You set the edges
field on other cities, but not on those.