2021-03-29

在创建邻接表的时候数组引用报出了空指针异常,找不出哪里错了,求大神

package Graph.Save;
//利用邻接表来实现无向图


import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.Scanner;

//这个是表头结点
class VertexNode{
       public String vertex;   //每个阶的数值
       public EdgeNode firstedge;   //边表头指针

       public Integer n1;//用来表示数组头的大小
}
//这个是结点链表
class EdgeNode{
    public Integer agjvex;   //结点下标
    public EdgeNode next;   //指向下一个结点

    //构造方法,对其进行赋值
    public EdgeNode(Integer n) {
        this.agjvex = n;
        this.next = null;
    }
}
//这个类的邻接表
class ALGraph{
   public VertexNode[] AdjList=new VertexNode[100];   //建立邻接表
    public int n,e;    //建立具体的顶点和边

    public ALGraph() {
        for(VertexNode a:AdjList){
            a.vertex=new String();
            a.firstedge=null;
        }
    }


    //根据顶点数值来查找顶点下标
    public int LocateIndex(String s){
        for(int i=0;i<n;i++)
            if(AdjList[i].vertex.equals(s))
                return i;

        return -1;
    }
}
public class Adjacency_table {
    public static void main(String[] args) {
        ALGraph G=new ALGraph();
        System.out.println(G.AdjList[2]);
        System.out.println("请输入顶点个个数和边的个数:");

        Scanner scanner=new Scanner(System.in);
        G.n=scanner.nextInt();
        G.e=scanner.nextInt();

        System.out.println("请输入各个顶点的值:");
        //1、首先对数组头进行赋值
        for(int i=0;i<G.n;i++){
            G.AdjList[i].vertex=scanner.next();

        }

        //2、创建邻接表
        for(int i=0;i<G.e;i++){
            //根据两条边来创建边结点
            System.out.println("请输入边的两个顶点:");
            String a1,a2;
            a1=scanner.next();
            a2=scanner.next();
            int n1,n2;
            n1=G.LocateIndex(a1);
            n2=G.LocateIndex(a2);
            EdgeNode edge=new EdgeNode(n2);
            edge.next=G.AdjList[n1].firstedge;
            G.AdjList[n1].firstedge=edge;
        }
    }
}

这里是警报

Exception in thread "main" java.lang.NullPointerException
	at Graph.Save.ALGraph.<init>(Adjacency_table.java:34)
	at Graph.Save.Adjacency_table.main(Adjacency_table.java:51)

Process finished with exit code 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值