Reading strings from a file and creating Adjacency lists for Graphs
<p>Newbie here!!. For my project, I have to read data as string from a file and create a graph for BFS, with adjacency list.I'm reading each line from the file and taking the first string as key and next string as it neighbor. But I can't understand, why I get null pointer exception.Please take a look at my code. My file looks like this</p>

<p>city.txt</p>

<pre><code>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
</code></pre>

<p>And my Graph.java file is </p>

<pre><code>import java.io.*;
import java.util.*;

public class Graph {

    Map&lt;String, LinkedList&lt;String&gt;&gt; adj;

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

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

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

    public List&lt;String&gt; getNeighbors(String v) {
       return adj.get(v);
    }

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

        while (iterator.hasNext()) {
           String key = iterator.next().toString();
           LinkedList&lt;String&gt; 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 &lt; 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 &lt; 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();
     }

  }
</code></pre>

<p>I am getting a null pointer exception error in the line g.addNeighbor(var_1, var_2) how do I solve the problem. Can somebody suggest some solution for this.</p>

<p>EDIT..</p>

<p>I also using <code>getKeyValuePairs()</code> to view the adjacency list but I'm getting some weird output. Any possible suggestion why this happens?</p>

<p>output:</p>

<pre><code> [city2]
city1 [city2, city2]
city2 [city2, city2, city2, city2]
city3 [city2, city2]
city4 [city2, city2]
city5 [city2, city2]
</code></pre>