title: Trying to implement a trie data structure in java
body: <p>I am trying to make a TrieNode class. Every node has a letter, links(the other nodes it connects to), a boolean declaring if this node marks the end of a full word, and I am trying to add another boolean saying weather it is part of a valid prefix. The part I am having trouble with is the prefix part. I am trying to make an isValidPrefix method.</p>
  
  <p>My TrieNode class:</p>
  
  <pre><code>class TrieNode
  {
      char letter;
      TrieNode[] links;
      boolean fullWord;
      boolean validPrefix;
  
      TrieNode(char letter)
      {
          this.letter = letter;
          links = new TrieNode[26];
          for(int i=0;i&lt;26;i++){//i keep getting a nullPointer exception
              this.links[i].validPrefix=false;
          }
          this.fullWord = false;
          this.validPrefix=true;
      }
  }
  </code></pre>
  
  <p>In my add method, every time I add a node, I set that nodes <code>validPrefix</code> to true.</p>
  
  <p>My is valid prefix method:</p>
  
  <pre><code> public boolean isValidPrefix(TrieNode root, String word) {
      int length = word.length();
      char[] letters = word.toCharArray();
      TrieNode curNode = root;
      for (int i = 0; i &lt; length; i++){
          curNode = curNode.links[letters[i]-97];
      }
      return curNode.validPrefix;//get a nullPointerException
  }
  </code></pre>
  
  <p>Here is my add method for reference </p>
  
  <pre><code>    public void insertWord(TrieNode root, String word){//97 is ascii value
      int length = word.length();
      char[] letters = word.toCharArray();
      TrieNode curNode = root;
  
      for (int i = 0; i &lt; length; i++){
          if (curNode.links[letters[i]-97] == null)
              curNode.links[letters[i]-97] = new TrieNode(letters[i]);
          curNode = curNode.links[letters[i]-97];
          curNode.validPrefix=true;
      }
      curNode.fullWord = true;  
  }
  </code></pre>