title: "Cannot read the array length because "contents" is null" in word unscrambler
body: <p>I'm trying to program a word unscrambler in java, but I keep getting this error <strong>Cannot read the array length because &quot;contents&quot; is null</strong>. I tried to use java.io.File, but it also gave me the same error when I tried it.</p>
  <pre><code>public static void main(String[] args) throws FileNotFoundException {
  
          String[] contents = txtToString(&quot;allwords.txt&quot;);
          Scanner in = new Scanner(System.in);
          System.out.println(&quot;Insert your letters&quot;);
          String input = in.nextLine();
          char[] inputArray = input.toCharArray();
   
          for(int i = 0; i &lt; contents.length; i++)
          {
              for(int j = 0; j &lt; contents[i].length()-1; j++)
              {
                  char[] word = contents[i].toCharArray();
   
                  for(int c = 0; c &lt; word.length-1; c++)
                  {
                      while(c&lt;inputArray.length)
                      {
                         if(word[c]==inputArray[c])
                          {
                             word[j]=' ';
                             for(int s = 0; s &lt; word.length-1;s++)
                             {
                                 if(word[s]!= ' ')
                                 {    
                                     break;
                                 }
                                 else
                                 {
                                      System.out.println(&quot;The word is &quot;+contents[i]);
                                      break;
                                 }
                             }
                          }
                      }
                  }
              }
          }
      }
  private static String[] txtToString(String file) {
      int count = 0;
      try {
          Scanner g = new Scanner(new File (file));
          while(g.hasNextLine()) {
              count++;
          }
          String[] textToArray = new String[count];
          Scanner helloReader = new Scanner(new File(file));
          for(int z = 0; z &lt; count; z++) {
              textToArray[z] = helloReader.next();
          }
          
          return textToArray;
      }
      catch (FileNotFoundException e){
          
      }
      return null;
  }
  </code></pre>
  <p>Why am I getting this error?</p>