SmallWorldExample的实现

时间:2013-11-28 17:29:48

标签: java graph jung

我对如何实现一些JUNG类感到很困惑。我正在尝试用“KleinbergSmallWorldGenerator”创建一个图形,但我不确定如何正确初始化。

我的代码:

Factory<? extends Graph<Integer, Integer>> graph_factory;
Factory<Integer> vertexFactory;
Factory<Integer> edgeFactory;

KleinbergSmallWorldGenerator<Integer,Integer> smallWorld;

public GraphView() {
    smallWorld = new KleinbergSmallWorldGenerator<Integer,Integer>(graph_factory, vertexFactory, edgeFactory, 20, 3) ;
    Graph2 = smallWorld.create();
}

即使我graph_factory.create();

,我也会收到“java.lang.NullPointerException”

帮助我理解;谢谢!

2 个答案:

答案 0 :(得分:1)

施工时

graph_factoryvertexFactoryedgeFactory都是null。这是因为他们使用their initial value, per the JLS

在构造函数中,我想你想传递这些对象的实例,而不是在你的无参数构造函数中初始化它们。

答案 1 :(得分:0)

示例:

static class GraphFactory implements Factory<Graph<Integer,String>> {
        public Graph<Integer,String> create() {
            return new SparseMultigraph<Integer,String>();
        }
    }

    static class VertexFactory implements Factory<Integer> {
        int a = 0;
        public Integer create() {
            return a++;
        }

    }
    static class EdgeFactory implements Factory<String> {
        char aa = 'a';
        public String create() {
            return Character.toString(aa++);
        }

    }

然后你召集这个例子

Graph<Integer, String>  Eppstein = new EppsteinPowerLawGenerator<Integer,String>(new GraphFactory(), new VertexFactory(), new EdgeFactory(), 100, 400, 100).create();