我正在尝试创建一个正常的图形,并使用Gephi工具包将其流式传输到Gephi GUI。我遵循工具包和流媒体插件教程。我很难让我的代码工作,因为查找API没有返回有效值。在调试时,我可以发现下面的两个Lookup方法都返回空值,因为我在使用这些对象访问其他方法时遇到了麻烦。
StreamingServer server = Lookup.getDefault().lookup(StreamingServer.class);
ServerControllerFactory = Lookup.getDefault().lookup(ServerControllerFactory.class);
由于'server'或'controllerFactory'都是null,使用'server'或'controllerFactory'对象访问任何方法都会抛出NullPointerException。例如,at:
ServerController ServerController = controllerFactory.createServerController(图);服务器。注册(serverController、上下文);
这是我的代码:
//Init a project - and therefore a workspace
ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
pc.newProject();
Workspace workspace = pc.getCurrentWorkspace();
// Get the graph instance
GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
GraphModel graphModel = graphController.getModel();
Graph graph = graphModel.getGraph();
//Add sample graph structure data
Node n1;
Node n2;
n1 = graphModel.factory().newNode();
n2 = graphModel.factory().newNode();
Edge edge = graphModel.factory().newEdge(n1, n2);
graph.addNode(n1);
graph.addNode(n2);
graph.addEdge(edge);
n1.getNodeData().setLabel("Node 1");
n2.getNodeData().setLabel("Node 2");
edge.getEdgeData().setLabel("Edge 1");
System.out.println(graph.getNodeCount() + " nodes");
System.out.println(graph.getEdgeCount() + " edges");
System.out.println();
StreamingServer server = Lookup.getDefault().lookup(StreamingServer.class);
ServerControllerFactory controllerFactory = Lookup.getDefault().lookup(ServerControllerFactory.class);
ServerController serverController = controllerFactory.createServerController(graph);
String context = "/mycontext";
server.register(serverController, context);
如果无法访问gephi工具包的META-inf/services
文件夹,则可能发生此情况;例如,当你合并jar;等,尤其是在您尝试进行服务器端部署时。
注。类文件在那里;所以没有类未找到异常被抛出;但服务无法定位;更多信息请参见http://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html