title: Creating Trie data structure in Java - NullPointerException
body: <pre><code>package trie;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  
  class Node {
      private String word;
      private HashMap&lt;Character, Node&gt; nodes;
  
      public List&lt;String&gt; getAll() {
          List&lt;String&gt; x = new ArrayList&lt;String&gt;();
          for (HashMap.Entry&lt;Character, Node&gt; entry : nodes.entrySet()) {
              Character key = entry.getKey();
              Node node = entry.getValue();
              if (node.word != null)
                  x.add(node.word);
              x.addAll(node.getAll());
          }
          return x;
      }
  
      public String toString() {
          return this.word;
      }
  
      public HashMap&lt;Character, Node&gt; getNodes() {
          return this.nodes;
      }
  
      public boolean insert(String word, int stringPos) {
          this.word = word;
          this.nodes = new HashMap&lt;Character, Node&gt;();
  
          Character currentLetter = word.charAt(stringPos);
          if (nodes.containsKey(currentLetter)) {
              nodes.put(currentLetter, new Node());
          }
  
          if (stringPos + 1 == word.length()) {
              nodes.get(currentLetter).word = word;
          } else {
              nodes.get(currentLetter).insert(word, stringPos);
          }
          return true;
      }
  
      public List&lt;String&gt; getAllWithPrefix(String prefix, int stringPos) {
          List&lt;String&gt; x = new ArrayList&lt;String&gt;();
          for (HashMap.Entry&lt;Character, Node&gt; entry : nodes.entrySet()) {
              Character key = entry.getKey();
              Node node = entry.getValue();
              if (stringPos &gt;= prefix.length() || key.equals(prefix.charAt(stringPos))) {
                  if (node.word != null) {
                      x.add(node.word);
                  }
  
                  if (node.nodes.size() == 0) {
                      if (stringPos + 1 &lt;= prefix.length()) {
                          x.addAll(node.getAllWithPrefix(prefix, stringPos + 1));
                      } else {
                          x.addAll(node.getAllWithPrefix(prefix, stringPos));
                      }
                  }
              }
          }
          return x;
      }
  }
  
  public class Trie {
      private Node root;
      public Trie() {
          this.root = new Node();
      }
  
      public void insert(String word) {
          root.insert(word, 0);
      }
  
      public List&lt;String&gt; getAll() {
          return root.getAll();
      }
  
      public List&lt;String&gt; getAllWithPrefix(String prefix, int stringPos) {
          return root.getAllWithPrefix(prefix, stringPos);
      }
  
      public static void main(String[] args) {
          Trie trie = new Trie();
          trie.insert("java");
      }
  }
  </code></pre>
  
  <p>OUTPUT:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at trie.Node.insert(Trie.java:47)
      at trie.Trie.insert(Trie.java:83)
      at trie.Trie.main(Trie.java:96)
  </code></pre>
  
  <p>I am getting <code>NullPointerException</code> as soon as run the program. I understand that this exception occurs when the object points to <code>null</code>. But in the constructor of <code>Trie</code> class I creating a new object of <code>Node</code>, so why would this happen?</p>