0

Here I am using a different class to implement graph using adjacency List.I included almost everything that is there, but still i am getting this error

public class Breadth_first_Traversal {  
Graph g;
void BFS(int s,int v){
    boolean[] visited=new boolean[v]; 
    LinkedList<Integer> q1=new LinkedList<Integer>();
    q1.add(s);
    visited[s]=true;
    while(q1.size()!=0){
        s=q1.poll();
        System.out.print(s+" ");
        Iterator<Integer> i=g.adj[s].listIterator();
        while(i.hasNext()){
            int n=i.next();
            if(!visited[n]){
                q1.add(n);
                visited[n]=true;
            }
        }
    }
}
public static void main(String args[]){
    Breadth_first_Traversal bfs=new Breadth_first_Traversal();
    Graph gr=new Graph(4);
    gr.addEdge(0, 1);
    gr.addEdge(0, 2);
    gr.addEdge(1, 2);
    gr.addEdge(2, 0);
    gr.addEdge(2, 3);
    gr.addEdge(3, 3);
    bfs.BFS(2, 4);
}

Output

2 Exception in thread "main" java.lang.NullPointerException
at Graph_data_structure.Breadth_first_Traversal.BFS(Breadth_first_Traversal.java:21)
at Graph_data_structure.Breadth_first_Traversal.main(Breadth_first_Traversal.java:40)Picked up _JAVA_OPTIONS: -Xmx512MJava Result: 1BUILD SUCCESSFUL (total time: 1 second)

Please help.

Dominique Fortin
  • 2,212
  • 15
  • 20
mj343
  • 1
  • 1

0 Answers0