java.lang.InstantiationException using jGraphT when trying to display the Weight of a lable

1.3k views Asked by At

Hi everyone I'm tring to display a graph using jGraphT. Instead of labling an edge with the name of the source and target notes I want to display the weight of the edge. For that, I created an aditional class to overwrite the toString method of DefaultWeightedEdge this way

    class MyWeightedEdge extends DefaultWeightedEdge {
      @Override
      public String toString() {
        return Double.toString(getWeight());
      }
    }

and instead of using DefaultWeightedEdge I use MyWeightedEdge everywhere, but I'm getting a java.lang.InstantiationException

Exception in thread "main" java.lang.RuntimeException: Edge factory failed
at org.jgrapht.graph.ClassBasedEdgeFactory.createEdge(ClassBasedEdgeFactory.java:86)
at org.jgrapht.ext.JGraphModelAdapter$ShieldedGraph.addEdge(JGraphModelAdapter.java:1091)
at org.jgrapht.ext.JGraphModelAdapter.handleJGraphInsertedEdge(JGraphModelAdapter.java:506)
at org.jgrapht.ext.JGraphModelAdapter.handleJGraphChangedEdge(JGraphModelAdapter.java:474)
at org.jgrapht.ext.JGraphModelAdapter$JGraphListener.handleChangedEdges(JGraphModelAdapter.java:964)
at org.jgrapht.ext.JGraphModelAdapter$JGraphListener.graphChanged(JGraphModelAdapter.java:893)
at org.jgraph.graph.DefaultGraphModel.fireGraphChanged(Unknown Source)
at org.jgraph.graph.DefaultGraphModel$GraphModelEdit.execute(Unknown Source)
at org.jgraph.graph.DefaultGraphModel.insert(Unknown Source)
at org.jgrapht.ext.JGraphModelAdapter.internalInsertCell(JGraphModelAdapter.java:769)
at org.jgrapht.ext.JGraphModelAdapter.handleJGraphTAddedEdge(JGraphModelAdapter.java:651)
at org.jgrapht.ext.JGraphModelAdapter$JGraphTListener.edgeAdded(JGraphModelAdapter.java:1036)
at org.jgrapht.graph.DefaultListenableGraph.fireEdgeAdded(DefaultListenableGraph.java:317)
at org.jgrapht.graph.DefaultListenableGraph.addEdge(DefaultListenableGraph.java:182)
at Visual.JGraphAdapterDemo.init(JGraphAdapterDemo.java:135)
at Visual.JGraphAdapterDemo.<init>(JGraphAdapterDemo.java:89)
at world.Conection.paint(Conection.java:268)
at world.Conection.<init>(Conection.java:56)
at world.Conection.main(Conection.java:44)
Caused by: java.lang.InstantiationException: Visual.JGraphAdapterDemo$MyWeightedEdge
    at java.lang.Class.newInstance(Unknown Source)
    at     org.jgrapht.graph.ClassBasedEdgeFactory.createEdge(ClassBasedEdgeFactory.java:84)
    ... 18 more

I will appreciate any help. Thank you.

2

There are 2 answers

0
Shaun On

In this case, you can try to add a constructor in your derived class as following:

class MyWeightedEdge extends DefaultWeightedEdge {
  //add this constructor
  public MyWeightedEdge() {
    super();
  }

  @Override
  public String toString() {
    return Double.toString(getWeight());
  }
}

In my case, it worked well, hope it will work for you as well.

1
grasshopper On

I had the exact same problem. I had the code for MyWeightedEdgeClass inside another class file. I put it in its own file (as a public class) and then the ClassBasedEdgeFactory could see it and instantiate it.