2016-09-11 3 views
1

새내기 !! !! 내 프로젝트의 경우 파일에서 문자열로 데이터를 읽고 인접 목록이있는 BFS 용 그래프를 만들어야합니다. 파일에서 각 행을 읽고 첫 번째 문자열을 키로, 다음 문자열을 이웃으로 사용합니다. 하지만 왜 널 포인터 예외가 발생하는지 이해할 수 없습니다. 제 코드를 살펴보십시오. 내 파일이파일에서 문자열 읽기 및 그래프 인접 목록 만들기

city.txt

city1 city2 10 
city1 city3 15 
city2 city1 5 
city2 city3 6 
city2 city4 22 
city2 city5 1 
city3 city4 9 
city3 city5 16 
city4 city1 4 
city4 city2 8 
city5 city2 1 
city5 city3 13 

처럼 보인다 그리고 내 Graph.java 파일

import java.io.*; 
import java.util.*; 

public class Graph { 

    Map<String, LinkedList<String>> adj; 

    public Graph() { 
    // TODO Auto-generated constructor stub 
    } 

    public Graph(String[] nodes) 
    { 
     adj = new HashMap<String, LinkedList<String>>(); 
     for (int i = 0; i < nodes.length; ++i) 
     { 
      adj.put(nodes[i], new LinkedList<String>()); 
     } 
    } 

    public void addNeighbor(String v1,String v2) { 
     adj.get(v1).add(v2); 
    } 

    public List<String> getNeighbors(String v) { 
     return adj.get(v); 
    } 

    public void getKeyValuePairs() 
    { 
     Iterator iterator = adj.keySet().iterator(); 

     while (iterator.hasNext()) { 
      String key = iterator.next().toString(); 
      LinkedList<String> value = adj.get(key); 
      System.out.println(key + " " + value); 
     } 
    } 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
     File file = new File("city.txt"); 
     FileReader fr = new FileReader(file); 
     BufferedReader br = new BufferedReader(fr); 
     String line = br.readLine(); 
     String [] tokens = line.split("\\s+"); 
     String [] nodes = new String[tokens.length]; 
     for (int i = 0; i < nodes.length; ++i) { 
      nodes[i] = tokens[i]; 
     } 

     Graph g = new Graph(nodes); 
     String var_1 = tokens[0]; 
     String var_2 = tokens[1]; 
     //String var_3 = tokens[2]; 



     while((line = br.readLine()) != null) 
     { 

      tokens = line.split("\\s+"); 
      nodes = new String[tokens.length]; 
      for (int i = 0; i < nodes.length; ++i) { 
       nodes[i] = tokens[i]; 
      } 

      var_1 = tokens[0]; 
      var_2 = tokens[1]; 
      //String var_3 = tokens[2]; 

      g.addNeighbor(var_1, var_2); 

     } 
     g.getKeyValuePairs(); 
     br.close(); 
    } 

    } 

내가 줄 g.addNeighbor에 널 포인터 예외 오류를 얻고있다 (VAR_1입니다 var_2) 어떻게 문제를 해결할 수 있습니까? 누군가가이를위한 몇 가지 해결책을 제안 할 수 있습니까?

편집 ..

나는 또한 인접 목록을 볼 수 getKeyValuePairs()를 사용하지만 몇 가지 이상한 결과를 얻고있다. 이것이 일어나는 이유는 무엇입니까?

출력 : 당신의 HashMap의 형용사가 정점 VAR_1를 포함하지 않는

[city2] 
city1 [city2, city2] 
city2 [city2, city2, city2, city2] 
city3 [city2, city2] 
city4 [city2, city2] 
city5 [city2, city2] 

답변

1

때문입니다. 첫 번째 선의 정점 만 그래프에 추가합니다.

편집 :

public class Graph 
{ 
    Map<String, LinkedList<String>> adj; 

    public Graph() { 
     adj = new HashMap<String, LinkedList<String>>(); 
    } 

    public void addNode(String node) 
    { 
     adj.putIfAbsent(node, new LinkedList<String>()); 
    } 

    public void addNeighbor(String v1,String v2) { 
     adj.get(v1).add(v2); 
    } 

    public List<String> getNeighbors(String v) { 
     return adj.get(v); 
    } 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
     File file = new File("city.txt"); 
     FileReader fr = new FileReader(file); 
     BufferedReader br = new BufferedReader(fr); 
     String line; 
     String [] tokens; 

     Graph g = new Graph(); 
     while((line = br.readLine()) != null) 
     { 
      tokens = line.split("\\s+"); 
      g.addNode(tokens[0]); 
      g.addNode(tokens[1]); 
      g.addNeighbor(tokens[0], tokens[1]); 
     } 
     br.close(); 
    } 
} 
1

당신은 당신의 Map에 추가하는 것을 잊었다 : 다음 작업 샘플입니다.

if(adj.get(v1) == null) { 
    adj.put(v1, new LinkedList<String>()); 
} 

전체 코드

import java.io.*; 
import java.util.*; 

public class Main { 

    private Map<String, LinkedList<String>> adj; 

    public Main() { 
     // TODO Auto-generated constructor stub 
    } 

    public Main(String[] nodes) 
    { 
     adj =new HashMap<String, LinkedList<String>>(); 
     System.out.println("nodes" + nodes.length); 
     for (int i = 0; i < nodes.length; ++i) 
     { 
      adj.put(nodes[i], new LinkedList<String>()); 
     } 
    } 

    public void addNeighbor(String v1,String v2) { 
     if(adj.get(v1) == null) { 
      adj.put(v1, new LinkedList<String>()); 
     } 
     adj.get(v1).add(v2); 
    } 

    public List<String> getNeighbors(String v) { 
     return adj.get(v); 
    } 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
     File file = new File("city.txt"); 
     FileReader fr = new FileReader(file); 
     BufferedReader br = new BufferedReader(fr); 
     String line = br.readLine(); 
     String [] tokens = line.split("\\s+"); 
     String [] nodes = new String[tokens.length]; 
     for (int i = 0; i < nodes.length; ++i) { 
      nodes[i] = tokens[i]; 
     } 

     Main g = new Main(nodes); 
     String var_1 = tokens[0]; 
     String var_2 = tokens[1]; 
     //String var_3 = tokens[2]; 

     while((line = br.readLine()) != null) 
     { 
      System.out.println(" test " + line); 
      tokens = line.split("\\s+"); 
      nodes = new String[tokens.length]; 
      for (int i = 0; i < nodes.length; ++i) { 
       nodes[i] = tokens[i]; 
      } 
      var_1 = tokens[0]; 
      var_2 = tokens[1]; 
      g.addNeighbor(var_1, var_2); 

     } 
     br.close(); 
    } 

} 
+0

I 출력이 정확하지 왜 제안을 제공 할 수있는 질문에 대한 편집을 한 다음은지도에 추가하는 버그 수정과 코드입니다. – Karthi13

+0

해결 된 항목 ... while 루프에서 var_2 변수에 주석을 달았습니다. – Karthi13

관련 문제