Question

Why am I getting a NULL pointer error for this program, as far as I can see everything looks fine, I've just started using JGraphT and need a little help with it.

Context: I need to be able to add flights to the graph, each flight is bi-directional and the flights should be weighted with their prices.

Error:

run:
Please enter the number of flights: 
2
Please enter the flight destination for flight 1:
Edinburg
Please enter the flight destination for flight 2:
Heathrow
Enter the edges
Edinburg
Heathrow
Please enter a price for this edge:
25.99
Exception in thread "main" java.lang.NullPointerException
    at org.jgrapht.graph.AbstractBaseGraph.setEdgeWeight(Unknown Source)
    at graphapp.MyGraph.setEdgeWeight(MyGraph.java:25)
    at graphapp.GraphApp.main(GraphApp.java:33)
Java Result: 1
BUILD SUCCESSFUL (total time: 14 seconds)

MyGraph.java (Class):

package graphapp;

import org.jgrapht.*;
import org.jgrapht.graph.*;
import org.jgrapht.alg.KruskalMinimumSpanningTree;

public class MyGraph {

    private final SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g = new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
    static final double DEFAULT_EDGE_WEIGHT=19;
    //DefaultWeightedEdge > (DefaultWeightedEdge.class); 
    private DefaultWeightedEdge e1;

    public void addVertex(String name) {
        g.addVertex(name);
        //graph.addVertex(name);
    }

    public void addEdge(String v1, String v2) {
        g.addEdge(v1, v2);
        e1 = g.addEdge(v1, v2);
    }

    public void setEdgeWeight(String EDGE_WEIGHT) {
        g.setEdgeWeight(e1, Double.valueOf(EDGE_WEIGHT));          
    }

    public SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> getGraph() {
        return g;
    }

    /*public SimpleWeightedGraph<String,DefaultWeightedEdge> getGraph() {
        return graph;
    }*/

    public void getSpanningTree() {
        KruskalMinimumSpanningTree k = new KruskalMinimumSpanningTree(g);
        System.out.println(k.getEdgeSet().toString());
        //KruskalMinimumSpanningTree k1=new KruskalMinimumSpanningTree(graph);
        //System.out.println(k1.getEdgeSet().toString());   
    }

    public void getSpanningTreeCost() {
        KruskalMinimumSpanningTree k = new KruskalMinimumSpanningTree(g);
        System.out.println(k.getSpanningTreeCost());
    }
}

GraphApp (Main):

package graphapp;
import org.jgrapht.demo.*;
import java.util.Scanner;
import graphapp.*;
public class GraphApp{

    public static void main(String args[]) {

        int x;
        Scanner sc = new Scanner(System.in);
        MyGraph my = new MyGraph();
        System.out.println("Please enter the number of flights: ");
        int no_of_ver = sc.nextInt();

        for(int i=1;i <= no_of_ver;i++) {
            System.out.println("Please enter the flight destination for flight "+i+ ":");
            my.addVertex(sc.next());
        }

        do {
            System.out.println("Enter the edges");
            String e1 = sc.next();
            String e2 = sc.next();
            my.addEdge(e1, e2);

            System.out.println("Please enter a price for this edge:");
            my.setEdgeWeight(sc.next());
            System.out.println("Continue... Yes:1 ********** No:0");
            x=sc.nextInt();
        } while(x==1);

        System.out.println("Graph\n" + my.getGraph().toString());
        System.out.println("\n\n**********Spanning Tree*********");
        my.getSpanningTree();
        System.out.println("\nSpanning Tree Cost");
        my.getSpanningTreeCost();
    }
}
Était-ce utile?

La solution

My error was with this method:

public void addEdge(String v1, String v2) {
    g.addEdge(v1, v2);
    e1 =g.addEdge(v1, v2);
    System.out.println("Edge added: " + e1.toString());
}

I needed to change it to this:

public void addEdge(String v1, String v2) {
    e1 =g.addEdge(v1, v2);
    System.out.println("Edge added: " + e1.toString());
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top