I am trying to write an program which performs a breath first search on a graph and returns a tree.
Here is the main method
The error which it returns
If you need additional info just ask?
Code:
class Alist{ int x; Alist next; public Alist (){ } public Alist(int x){ this.x = x; next = null; } }
Code:
//Under Graph class protected Alist list; int parent[]; public void removeFirst(){ list = list.next; } public int first(){ return list.x; } public void addLast(int x){ Alist curr = list; Alist temp = new Alist(x); while(curr.next != null){ curr = curr.next; } curr.next = temp; } public boolean empty(){ if(list != null){ return false; } return true; } public void BreathFirst(int v){ int parent[] = new int[numvertices]; boolean marked[] = new boolean[numvertices]; for(int i=0;i<numvertices;i++){ marked[i] = false; } list = new Alist(v); marked[v] = true; int w; while (!empty()){ w = first(); removeFirst(); for(int x=0;x<numvertices;x++){ if(isedge(w,x) && !marked[x]){ marked[x] = true; parent[x] = w; addLast(x); } } } for(int i=0;i<numvertices;i++){ System.out.println(parent[i]+" "+i); } }
Code:
Graph g; int n; SimpleInp inp = new SimpleInp("E:/graph.txt"); n = inp.readInt(); g = new Graph(n, false); g.read(inp); g.BreathFirst(0);//Everything works except this point
Code:
Exception in thread "main" java.lang.NullPointerException at Graph.addLast(Graph.java:292) at Graph.BreathFirst(Graph.java:320) at Driver.main(Driver.java:19)
Comment