2013-04-02 80 views
-1

我想在我從一個文本文件「Input.txt」的鄰接列表創建的圖上執行呼吸第一次搜索我在第162行得到一個NullPointerException,它說「root.visited = true;「我無法弄清楚這是爲什麼。這是我的代碼。NullPointerException Java BFS

 import java.io.BufferedReader; 
    import java.io.FileNotFoundException; 
    import java.io.FileReader; 
    import java.io.IOException; 
    import java.util.ArrayList; 
    import java.util.Iterator; 
    import java.util.LinkedList; 
    import java.util.Queue; 
    import java.util.Stack; 

    public class BFS { 
    public static void main(String[] args) { 
     { 
      Graph g = new Graph(); 
      // Making Adjacency Matrix 
      ArrayList<ArrayList<Integer>> adjacency = new ArrayList<ArrayList<Integer>>(); 
      adjacency.add(new ArrayList<Integer>()); 

      String input = "input.txt"; 
      Graph MyGraph = new Graph(); 
      boolean root = true; 
      Vertex n = null; 
      int j = 0; 
      try { 
       // Reading in the Input File to the Adjacency Matrix 
       BufferedReader reader = new BufferedReader(
         new FileReader(input)); 
       String line = reader.readLine(); 

       while (line != null) { 
        // Getting numbers from each line 
        String[] numbers = line.split(","); 
        int[] values = new int[numbers.length]; 
        for (int i = 0; i < numbers.length; i++) 
         values[i] += Integer.parseInt(numbers[i]); 

        // Adding values to Matrix 
        for (int i = 0; i < values.length; i++) { 
         adjacency.get(j).add(values[i]); 
         System.out.print(" " + adjacency.get(j).get(i)); // output 
                      // of 
                      // matrix 
        } 
        System.out.print("\n"); 

        // Progressing through file 
        adjacency.add(new ArrayList<Integer>()); 
        line = reader.readLine(); 
        j++; 
       } 

       // read each line 
       for (int currRow = 0; (line = reader.readLine()) != null; currRow++) { 
        String[] row = line.split(","); 

        // insert vertices 
        if (currRow == 0) { 

         for (int i = 0; i < row.length; i++) { 
          g.addVertex(n); 
          if (root = true) { 

          } 
         } 
        } 

        // create the edges specified in this row 
        for (int i = 0; i < row.length; i++) { 

         // edge exists between indices 'currRow' and col 'i'. 
         if (Integer.parseInt(row[i]) == 1) { 
          g.connectVertex(currRow, i); 
         } 
        } 
       } 
       reader.close(); 
      } catch (FileNotFoundException e) { 
       System.out.println(e); 
      } catch (IOException e) { 
       System.out.println(e); 
      } 

      /* DFS Algorithm */ 

      // schtuff (use stack? use index of node for unique number?) 

      // System.out.print("Visited nodes (in order): "); 

     } 

     // catch exceptions & errors 
     Graph MyGraph = new Graph(); 
     MyGraph.dfs(); 
    } 

    public static class Graph { 
     public Vertex root; 
     public ArrayList<Vertex> vertices = new ArrayList<Vertex>(); 
     public ArrayList<Vertex> dfsArrList = new ArrayList<Vertex>(); 
     public int[][] adjMatrix; 
     int size; 

     public void setRootVertex(Vertex n) { 
      this.root = n; 
     } 

     public Vertex getRootVertex() { 
      return this.root; 
     } 

     public void addVertex(Vertex n) { 
      vertices.add(n); 
     } 

     public void removeVertex(int loc) { 
      vertices.remove(loc); 
     } 

     public void connectVertex(int vertexStart, int vertexEnd) { 
      if (adjMatrix == null) { 
       size = vertices.size(); 
       adjMatrix = new int[size][size]; 
      } 

      int startIndex = vertices.indexOf(vertexStart) + 1; 
      int endIndex = vertices.indexOf(vertexEnd) + 1; 
      adjMatrix[startIndex][endIndex] = 1; 
      adjMatrix[endIndex][startIndex] = 1; 
     } 

     public void removeEdge(Vertex v1, Vertex v2) { 

      int startIndex = vertices.indexOf(v1); 
      int endIndex = vertices.indexOf(v2); 
      adjMatrix[startIndex][endIndex] = 1; 
      adjMatrix[endIndex][startIndex] = 1; 
     } 

     public int countVertices() { 
      int ver = vertices.size(); 
      return ver; 
     } 

     private Vertex getUnvisitedChildNode(Vertex n) { 

      int index = vertices.indexOf(n); 
      int j = 0; 
      while (j < size) { 
       if (adjMatrix[index][j] == 1 
         && vertices.get(j).visited == false) { 
        return vertices.get(j); 
       } 
       j++; 
      } 
      return null; 
     } 

     public Iterator<Vertex> dfs() { 

      Stack<Vertex> s = new Stack<Vertex>(); 
      s.push(this.root); 
      root.visited = true; 
      printVertex(root); 
      while (!s.isEmpty()) { 
       Vertex n = s.peek(); 
       Vertex child = getUnvisitedChildNode(n); 
       if (child != null) { 
        child.visited = true; 
        dfsArrList.add(child); 
        s.push(child); 
       } else { 
        s.pop(); 
       } 
      } 
      clearVertices(); 
      return dfsArrList.iterator(); 
     } 

     private void clearVertices() { 
      int i = 0; 
      while (i < size) { 
       Vertex n = vertices.get(i); 
       n.visited = false; 
       i++; 
      } 
     } 

     private void printVertex(Vertex n) { 
      System.out.print(n.vertexName + " "); 
     } 
    } 

    public class Vertex { 

     public char vertexName; 
     public boolean visited = false; 

     public Vertex(char l) { 
      this.vertexName = l; 
     } 

    } 

} 
+0

異常堆棧跟蹤在哪裏?僅提供相關代碼 –

+0

首先讓您更容易追蹤變量的變化:使其變爲私人。爲什麼你幾乎所有的變量都是公開的?下一步:你永遠不會顯示正在調用setRootVertex ... –

+0

你的'root'爲空。 – Shark

回答

1

看看這段代碼(其中main一個支撐區間之後略有怪異):

Stack<Vertex> s = new Stack<Vertex>(); 
s.push(this.root); 
root.visited = true; 

你期望MyGraph.root到:

Graph MyGraph = new Graph(); 
MyGraph.dfs(); 

dfs方法開頭在這裏?它怎麼可能是空值以外的任何東西? (。請注意,您有兩個MyGraph變量聲明中main第一個不用於任何東西,因爲某些原因)

我強烈建議你重組這些代碼顯著:

  • 重構您main方法要小一些
  • 避免引入一個新的作用域只是爲了它的緣故 - 事實上,你的主要方法是有效的:

    public static void main(String[] args) { 
        { 
         // Code 
        } 
    } 
    

    很奇怪。當你不

  • 避免嵌套類真正需要它們:使用單獨的文件
  • 避免公共變量
  • 讓您的數據不變,你可以。施工後你真的需要改變頂點的根嗎?你不能把它傳遞給構造函數嗎?像這樣的事情使代碼更容易理解。
+0

我喜歡你的速度回答細節。 –

+0

目前我只想將Vertex「n」設置爲除「Null」之外的其他值我希望能夠將它設置爲鄰接列表中的第一個頂點http://pastebin.com/ 5a0vbEjy – Cfox7

+0

Jon的確是另一個好答案。 – Shark

0

Vertex root沒有被初始化,以便root.visited變得在運行時null.visited引起NullPointerException。預計通過setRootVertex方法設定值。

0

當你調用
MyGraph.dfs();
public static class Graph { public Vertex root;

您的根目前沒有初始化。