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.

为什么我得到这个程序的NULL指针错误,只要我看到一切看起来很好,我刚开始使用JGraphT并需要一些帮助。

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

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

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

1 个解决方案

#1


5

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

更多相关文章

  1. 错误:警告:无法从javaldx读取路径
  2. Java错误:线程“main”中的异常java.lang.ArrayIndexOutOfBoundsE
  3. 不幸的是,在声明按钮时,模拟器中出现了错误
  4. 当我运行.jar时,在java.library中会得到一个“No lwjgl”。路线”
  5. 导入证书后的Java Keytool错误,“Keytool error: Java .io。FileN
  6. Cassandra Java驱动程序错误 - 所有主机尝试查询失败连接已关闭
  7. 线程“main”中的异常java.lang.RuntimeException:无法编译的源代
  8. java httpclient访问某些网页报403错误
  9. Java:IntelliJ想法生成的代码错误地为所有Class名称添加了其包名

随机推荐

  1. 使用 JWT、Redis、MySQL 存储 OAuth2.0
  2. 用 await/async 正确链接 Javascript 中
  3. 浅谈 JavaScript 中的垃圾收集器
  4. 上次是谁说搞不清学习路线来着?
  5. 如何让自己少写点bug?
  6. 盘点高级前端必备的 TOP 级知识点
  7. 真正了解 JavaScript,从 bind(),call() 和
  8. 为什么我喜欢JavaScript的Optional Chain
  9. JavaScript 创意卡通滑杆拖动控件
  10. 利用ELK搭建Docker容器化应用日志中心