问题
I'm trying to create a normal graph and stream it to Gephi GUI using the Gephi toolkit. I am following the the toolkit and the streaming plugin tutorials. I am having difficulty getting my code to work because the lookup API is not returning valid values. On debug, I could find that both the Lookup methods below return null values because of which I am having trouble in accessing other methods using these objects.
StreamingServer server = Lookup.getDefault().lookup(StreamingServer.class);
ServerControllerFactory controllerFactory = Lookup.getDefault().lookup(ServerControllerFactory.class);
As both 'server' or 'controllerFactory' are null, accessing any methods using 'server' or 'controllerFactory' objects would then throw NullPointerException. For example, at :
ServerController serverController = controllerFactory.createServerController(graph); server.register(serverController, context);
This is the code I have:
//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);
Any inputs to solve this would be helpful.
回答1:
This could happen if META-inf/services
folder of gephi toolkit is not accessible; e.g. when you merge jars; etc. especially common when you try to do server side deployment.
p.s. The class files are there; so no Class Not Found exception is thrown; but services are not being able to be located; for more info see http://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html
来源:https://stackoverflow.com/questions/23641359/streaming-graph-to-gephi-using-toolkit-nullpointerexception