title: How to fix NullPointerException in breath first search in java?
body: <p>I try to construct a breath first search relationship(BFS) by using a txt file. It works perfectly when the BFS size is small(eg. 7nodes), but NullPointerException appears when the BFS size is big(eg. 20000nodes). I've already read some posts about NullPointerException, but I still have no idea on how to solve it.  </p>
  
  <p>The exception message:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
          at GraphAdjacentList.setEdge(GraphAdjacentList.java:16)    
          at GraphAdjacentListDemo.main(GraphAdjacentDemo.java:52)
  </code></pre>
  
  <p>There are 7 nodes, then each line shows the relationship of two nodes.
  The format of the txt file:</p>
  
  <pre><code>7
  1 2
  1 3
  1 4
  4 5
  4 6
  5 7
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  
  public class GraphAdjacentListDemo{
  /* to build an adjacentList */
  final private Map&lt;Integer, List&lt;Integer&gt;&gt; Adjacent_List;
  public GraphAdjacentListDemo(int numberOfVertices){
      Adjacent_List = new HashMap&lt;Integer,List&lt;Integer&gt;&gt;();
      for(int i=1;i&lt;=numberOfVertices;i++){
          Adjacent_List.put(i,new LinkedList&lt;Integer&gt;());
      }
  }
  
  public void setEdge(int source,int destination){
      List&lt;Integer&gt; sList = Adjacent_List.get(source);
      sList.add(destination);
      List&lt;Integer&gt; dList = Adjacent_List.get(destination);
      dList.add(source);
  }
  
  public List&lt;Integer&gt; getEdge(int source){
      return Adjacent_List.get(source);
  }
  
  public static void main(String args[]) throws Exception{
      /* to import a node source file */
      File f = new File("/Users/apple/Desktop/Graph_Sample.txt");
      List&lt;Integer&gt; source = new ArrayList&lt;Integer&gt;();
      int count=0;
      Scanner sc = new Scanner(f);
  
  
      while(sc.hasNextInt()){
          int i=0;
          i = sc.nextInt();   
          source.add(i);
          count++;
          }
          sc.close();
  
      /* to store nodes into source_array and destination_array */
      int time=(count-1)/2;
      List&lt;Integer&gt; sourceList = new ArrayList&lt;Integer&gt;();
      List&lt;Integer&gt; destinationList = new ArrayList&lt;Integer&gt;();
      for(int i=1;i&lt;=time;i++){
          sourceList.add(source.get(1+2*(i-1)));
          destinationList.add(source.get(2+2*(i-1)));
      }
  
      GraphAdjacentList adjacentList = new GraphAdjacentList(source.get(0));
      for(int i=0;i&lt;sourceList.size();i++){
          adjacentList.setEdge(sourceList.get(i),destinationList.get(i));             
      }
  
      System.out.println("The graph is:\n");
      try{
      for(int i=1;i&lt;=source.get(0);i++){
          System.out.print(i+"--&gt;");
          List&lt;Integer&gt; edgeList = adjacentList.getEdge(i);
          for(int j=1;;j++){
              if(j != edgeList.size()){
                  System.out.print(edgeList.get(j-1)+"--&gt;");
              }else{
                  System.out.print(edgeList.get(j-1));
                  break;
              }
          }
          System.out.println();
          }
      }catch(InputMismatchException inputMismatch){}
  
  
  }
  
  }
  </code></pre>