title: Object reference not set to an instance of an object c# - code posted
body: <p>I kept trying to play with this code, but am getting,
  Object reference not set to an instance of an object
  one the marked arrow.  Can anyone look at what I'm doing wrong?  I've posted only the relevant code.  </p>
  
  <p>Thanks!         </p>
  
  <p>ERROR AT // MY ERROR HERE</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  using System.IO;
  namespace pkb
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;      
      /// 
  
  
      public partial class MainWindow : Window
      {
  
          string[] kbsubject = new string[4000];
          string[] kbbody = new string[4000];
          string[] wordsplit = new string[4000];
          int[] hits = new int[4000];
           StreamWriter WriteBody = new StreamWriter("kbsubjecttest.txt");
          StreamReader readSubject = new StreamReader("kbsubject.txt");
          StreamReader readBody = new StreamReader("kbbody.txt");
          int IndexHolder = 0, counter = 0, counterSearch = 0, WordsIndex = 0, counterWord=0, ArrayIndex = 0;
          string compareBody, compareSubject;
  
  
          public MainWindow()
          {
              InitializeComponent();
  
  
  
  
          }
  
          private void button1_Click(object sender, RoutedEventArgs e)
          {
              wordsplit = SearchBox.Text.Split(' ');
  
  
              WordsIndex = 0;
              counterWord = 0;
              ArrayIndex = 0;
              counterSearch = 0;
              diagWindow.Items.Add(" counter = " + counter);
              while (counter &gt; counterSearch)
              {                                                             // MY ERROR BELOW
                  if (kbbody[counterWord].Contains(wordsplit[ArrayIndex])) // MY ERROR HERE
                  {
                      hits[ArrayIndex] = counterWord;
                      diagWindow.Items.Add(hits[ArrayIndex] + " " + kbbody[ArrayIndex]);
                      ArrayIndex++;
  
                  }
  
                  counterWord++;
                  WordsIndex++;
                  counterSearch++;
                  diagWindow.Items.Add(" we are on index " + counterSearch);
  
  
  
  
  
              }
          }
          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
  
              compareBody = readBody.ReadLine();
              compareSubject = readSubject.ReadLine();
              if (compareBody == compareSubject)
              {
                  diagWindow.Items.Add(" Subject and Body Database compared successfully.");
                  IndexHolder = int.Parse(compareBody);
                  diagWindow.Items.Add(IndexHolder + " Index saved to variable.");
  
              }
              else
              {
                  diagWindow.Items.Add("Error with comparison");
                  diagWindow.Items.Add("------------------------------");
                  diagWindow.Items.Add("Body comparison has " + compareBody + " where Subject comparison has " + compareSubject);
              }
  
              //load information into arrays
  
  
  
              while (counter &lt;= IndexHolder)
              {
                  kbbody[counter] = readBody.ReadLine();
                  kbsubject[counter] = readSubject.ReadLine();
                  counter++;
  
  
              }
              diagWindow.Items.Add(counter + " Items successfully added into searchable database");
              diagWindow.Items.Add(" counter = " + counter);
  
  
  
  
  
          }
          }
      }
  </code></pre>