BFS方法反复显示nullpointerexception

时间:2019-05-17 06:54:54

标签: java nullpointerexception breadth-first-search

在我的作业中,我必须体现Bfs和Edge方法

在BFS方法中,我必须使用队列来存储访问的边缘

在Edge方法中,我必须存储arraymatrix

如果我输入,则显示nullpointerexception

void Bfs(int v) { 
    Queue<Integer> q = new LinkedList<>();

    q.add(v);
    for(int i=0;i<numofnodes;i++) {
        visited[i]=false;
    }
    visited[v] = true;

     while(!q.isEmpty()){
            int temp = q.poll();
            System.out.print(temp + " ");

            for(int j = 0; j <numofnodes; j++){
                if(AdjMatrix[temp][j] == 1 && visited[j]==false){                   
                    q.add(j);                       
                    visited[j]= true;            
                    }
                }               
            }
}

   void Edge(int n1, int n2) { 
    AdjMatrix[n1][n2]=1;
    AdjMatrix[n2][n1]=1;
}

java.lang.NullPointerException

  

i 5

     

e 0 1

     

e 0 3

     

e 1 2

     

e 1 4

     

e 2 3

     

e 3 4

     

bfs 0

     

0 1 3 2 4

1 个答案:

答案 0 :(得分:1)

它可能来自拼写错误:

for(int i=0;i<numofnodes;i++) {
    visited[v]=false;
}

应为:

for(int i=0;i<numofnodes;i++) {
    visited[i]=false;
}