1

I am trying to create star graph using JGraphT, however what I have written seems to be wrong for compiler. What I get is a NullPointerException at lines marked as NULL POINTER. Why is that?

private static DefaultWeightedEdge edge(int i, int j) {
    DefaultWeightedEdge graphEdge = Graph.getEdge(i,j);
    return graphEdge;
}

private static int vertex(int i) {
    int vertex = VertexArray[i-1];
    return vertex;
}

private static void setWeight(DefaultWeightedEdge edge, double weight) {
    Graph.setEdgeWeight(edge, weight); //NULL POINTER
}


private static SimpleWeightedGraph<Integer, DefaultWeightedEdge> createWeightedGraph() {
    Graph = new SimpleWeightedGraph<Integer, DefaultWeightedEdge>(DefaultWeightedEdge.class);

    for (int i = 0; i < numberOfVertices; i++) {
        VertexArray[i] = i+1;
        Graph.addVertex(vertex(i+1));
    }

    for (int i = 1; i <= numberOfEdges; i++) {
        DefaultWeightedEdge edge = Graph.addEdge(vertex(1), vertex(i+1)); 
    }

    for (int i = 1; i <= numberOfEdges; i++) {
        setWeight(edge(1, i), 0.95);} //NULL POINTER

   //I would like to start group of threads here
    service=Executors.newFixedThreadPool(manNum);
        for(int k=0; k < manNum; k++){
            ManufacturePattern manPat=new ManufacturePattern(k);
            service.submit(manPat);
        }

    return Graph;
}
prgst
  • 121
  • 1
  • 1
  • 11
  • 1
    Are you sure that `G.getEdge(i,j);` returns a value? PS: you should definitely work on your naming convention.... – Loci May 29 '15 at 10:04
  • I think so. I used similar scheme in other program and it was working there. I'll edit the names ;) – prgst May 29 '15 at 10:11
  • 1
    (Edited) When there is an exception in the first line marked with `NULL POINTER`, then this simply means that your `Graph` is `null`. If the exception actually comes from *inside* the `setEdgeWeight` method, then most likely your `edge` is `null` because there is no edge `1,i` in the graph. In doubt, provide a http://stackoverflow.com/help/mcve – Marco13 May 29 '15 at 10:44
  • Is there a reason for all that static hell? Without complete code, it is hard to tell what is causing Graph to be null. – Fildor May 29 '15 at 10:58
  • Do you want to know [what a NullPointerException is](http://stackoverflow.com/questions/17913640/what-is-a-java-nullpointerexception), or do you know but are wondering why? (Which I guess should be kind of clear if you know what it is.) – Jongware May 29 '15 at 10:59
  • I found the mistake. I was using wrong data under `numberOfEdges`. Now it is all fine ;) – prgst May 29 '15 at 12:04

0 Answers0