Can someone tell me were the problem is, Please?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thatos
    New Member
    • Aug 2007
    • 105

    Can someone tell me were the problem is, Please?

    I am trying to write an program which performs a breath first search on a graph and returns a tree.
    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);
    	  }
    	  
      }
    Here is the main method
    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
    The error which it returns
    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)
    If you need additional info just ask?
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Your error is on line 292, right?

    Do you know what a NullPointerExce ption is? It is the most common programming error.

    You are trying to dereference a null reference on that line.

    Comment

    • thatos
      New Member
      • Aug 2007
      • 105

      #3
      Originally posted by BigDaddyLH
      Your error is on line 292, right?

      Do you know what a NullPointerExce ption is? It is the most common programming error.

      You are trying to dereference a null reference on that line.
      Can you plsk tell me how can I resolve my problem,I know what NullPointerExce ption is, but I do not know were is it there cause list references something.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by thatos
        Can you plsk tell me how can I resolve my problem,I know what NullPointerExce ption is, but I do not know were is it there cause list references something.
        We're both in the dark. I don't know what line 292 is!-

        Comment

        • thatos
          New Member
          • Aug 2007
          • 105

          #5
          Originally posted by BigDaddyLH
          We're both in the dark. I don't know what line 292 is!-
          Here is line 292
          Code:
          list = new Alist(v);

          Comment

          • thatos
            New Member
            • Aug 2007
            • 105

            #6
            Originally posted by thatos
            Here is line 292
            Code:
            list = new Alist(v);
            I made a mistake the line 292 is
            Code:
            addLast(x);

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by thatos
              Here is line 292
              Code:
              list = new Alist(v);
              Are you sure it isn't:

              [CODE=Java]while(curr.next != null){[/CODE]

              ...because there is nothing in the line you quote that can cause a NullPointerExce ption!

              Suggestion: clean and build. Delete all your *.class files and recompile. This won't fix your bug but maybe something is out of synch.

              Comment

              • thatos
                New Member
                • Aug 2007
                • 105

                #8
                Originally posted by BigDaddyLH
                Are you sure it isn't:

                [CODE=Java]while(curr.next != null){[/CODE]

                ...because there is nothing in the line you quote that can cause a NullPointerExce ption!

                Suggestion: clean and build. Delete all your *.class files and recompile. This won't fix your bug but maybe something is out of synch.
                You are right, I changed addLast to this
                Code:
                public void addLast(int x){
                	  Alist curr = list;
                	  Alist temp = new Alist(x);
                	  while(curr != null){
                		  curr = curr.next;
                	  }
                	  curr = temp;
                	  
                  }

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  Originally posted by thatos
                  You are right, I changed addLast to this
                  Code:
                  public void addLast(int x){
                  	  Alist curr = list;
                  	  Alist temp = new Alist(x);
                  	  while(curr != null){
                  		  curr = curr.next;
                  	  }
                  	  curr = temp;
                  	  
                    }
                  Woo-hoo! I thought it was because curr was null!

                  Comment

                  Working...