title: C# Null Reference Exception while trying to split a string
body: <p>I'm trying to figure out how to resolve a NullReferenceException in my C# console application.</p>
  
  <p>It's at a basic level. </p>
  
  <p>Essentially, I have a method called getWord(). It takes two arguments- a string and an integer.</p>
  
  <p>getWord uses the integer and gets the word based on that integer from the string(based on spaces, no commas or any other types of punctuation)</p>
  
  <p>This is how it is supposed to work:</p>
  
  <p>getWord("john is seventeen", 0) results in "john"</p>
  
  <p>getWord("john is seventeen", 1) results in "is"</p>
  
  <p>getWord("john is seventeen", 2) results in "seventeen"</p>
  
  <p>In the program, I declare a simple string array, initialize it, and add two elements to it.</p>
  
  <p>I loop through each element of the array and print the first word from each element of the array - getWord(line, 0).</p>
  
  <p>The problem is that the console prints the first word of each line but after that I get a NullReferenceException on this line:</p>
  
  <pre><code>    string[] words = inputString.Split(new char[] { ' ' });
  </code></pre>
  
  <p>And I have no clue why. I'm using Visual C# 2008 and .NET Framework 3.5 if it matters. 
  I keep getting NullReferenceExceptions in all of my C# code and I have no idea why.</p>
  
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
  
      namespace ConsoleApplication1
      {
          class Program
          {
              public static string[] levelCurList;
              static void Main(string[] args)
              {
  
                  levelCurList = new string[500];
  
                  levelCurList[0] = "52 afdaf";
                  levelCurList[1] = "dfsf afdf";
  
                  foreach (string line in levelCurList)
                  {
                      Console.WriteLine(getWord(line, 0));
                  }
  
              }
              public static string getWord(string inputString, int word)
              {
                  string[] words = inputString.Split(new char[] { ' ' });
                  if (words.Length &gt;= word)
                      return words[word];  
                  else
                      return "";
              }
          }
      }
  </code></pre>