title: Gathering data from datagrid in a specified column and row
body: <p>In my WPF application I am browsing database in a datagrid. The point of my code is to store selected cells content in datagrid to some list of values for further operations. My code works well for selected items under 12, but for more items it throws an <code>NullRefferenceException</code> that says "</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object".</p>
  </blockquote>
  
  <p>Thank you for help.</p>
  
  <p>code:</p>
  
  <pre><code>List&lt;string&gt; graphValue = new List&lt;string&gt;(dataGrid1.SelectedItems.Count); //create list 
  IList someList = new ArrayList(dataGrid1.SelectedItems); //define Ilist
  DataGridColumn dataGridCol = dataGrid1.Columns[listBox1.SelectedIndex]; 
  //select column whom i wana collect data
  
  if (dataGrid1.SelectedItems != null)    //when selection applied..
  {
     for (int i = 0; i &lt; dataGrid1.SelectedItems.Count; i++) //go row by row in selected column above
     {
        try
        {
           id = ((TextBlock)dataGridCol.GetCellContent(someList[i])).Text.ToString(); //save cell content to string
           graphValue.Add(id); //add value to Ilist
        }
  
        catch (Exception ex)
        { 
           System.Windows.MessageBox.Show(ex.Message, "error"); }
        }
      }
    }
  </code></pre>