JGraphT cannot be cast to org.jgrapht.graph.DefaultWeightedEdge
-
29-01-2021 - |
Frage
I'm new Java and am using jGraphT to create a SimpleDirectedWeightedGraph. I'm getting this error when trying to set weights on my edges after creating and adding them to the graph:
Exception in thread "main" java.lang.ClassCastException: ObservationsDAG$ObservationsDAGEdge cannot be cast to org.jgrapht.graph.DefaultWeightedEdge
at org.jgrapht.graph.AbstractBaseGraph.setEdgeWeight(Unknown Source)
I am assuming I need to do something in my ObservationsDAGEdge class here, but from looking at the JGraphT docs, I am stuck as to what that is. Does my edge class need weight instance variable and do I need to provide getEdgeWeight() and setEdgeWeight()?
Keine korrekte Lösung
Andere Tipps
So, I figured this out. I first tried extending DefaultDirectedEdge, but then getEdgeTarget() stopped working - probably because I need to implement start/end vertices.
After reading the code, I tried subclassing SimpleDirectedWeightedGraph and overrode setEdgeWeight and getEdgeWeight and gave my edge class a weight instance variable.
That finally worked as sexpected.