2

为什么我会收到这个程序的 NULL 指针错误,据我所知,一切看起来都很好,我刚刚开始使用 JGraphT,需要一些帮助。

背景:我需要能够将航班添加到图表中,每个航班都是双向的,并且航班应该根据它们的价格加权。

错误:

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(类):

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(主要):

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();
    }
}
4

1 回答 1

5

我的错误是使用这种方法:

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

我需要将其更改为:

public void addEdge(String v1, String v2) {
    e1 =g.addEdge(v1, v2);
    System.out.println("Edge added: " + e1.toString());
}
于 2013-11-27T13:12:56.360 回答