title: C# - ListView Null exception
body: <p>I am struggling with ListViews for a while...I am writing a program which I cant copy in here (it has 637 lines long)...basically I am trying to delete the selected lines of a ListView...BUT if the ListView is big enough to show a scrolling bar it chashes the program saying that an "object reference was not set for an instance of an object", it actually don't completly crashes, but it shows an ugly JIT debugging window.
  I know that the problem is with the size of the ListView, because if I just try to delete any item of the ListView with an active scrolling bar, I get the crash, BUT if I just resize the window to be big enough to disable the scrolling bar (i.e. big enough so everything will fit and a scroll bar wont be necessary thus the system will not make one), everything works just fine!
  I am suspecting that maybe it is some internal bug of the .NET plataform....any ideas?</p>
  
  <p>(I already tried all kinds of codes for deleting selected items, every single one of them had this same error, but I made it work by copying all the ListView to an Array, and deleting the selected items in the array, and then copying them back to a clean ListView....weird, messy, ugly, I know, but it was the only code that worked...but now I am getting this same bug BUT only after installing the program, this is not happening when I run the program in Visual Studio!) </p>
  
  <p>EDIT:
  the delete code is (the commented part are all the codes I previously tried and failed in the same bug....this one only crashes when istalled, not in Visual Studio 2010...Framework is .NET 4.0):</p>
  
  <pre><code>private void limparLinhasSelecionadas() 
          {
              if (listView1.Items.Count &gt; 0 &amp;&amp; limpando == false)
              {
                  limpando = true;
                  int i = 0; 
  
                  //for (i = listView1.Items.Count - 1; i &gt;= 0 ; i--)
                  //{
                      //if (listView1.Items[i].Selected == true)
                      //{
                      //    listView1.Items[i].Remove();                       
                      //}
  
                  //}              
                  //////for (i = listView1.SelectedIndices.Count - 1; i &gt;= 0; i--)
                  //////{
                  //////    listView1.Items[listView1.SelectedIndices[i]].Remove();
                  //////}
                  //foreach (ListViewItem item in listView1.SelectedItems)
                  //{
                  //    listView1.Items.Remove(item);
                  //}
  
                  ArrayList itens = new ArrayList();
  
                  foreach (ListViewItem item in listView1.Items)
                  {
                      itens.Add(item);
                  }
  
                  foreach (ListViewItem item in listView1.SelectedItems)
                  {
                      itens.Remove(item);                    
                  }
  
                  listView1.Items.Clear();
  
                  foreach (ListViewItem item in itens)
                  {
                      listView1.Items.Add((ListViewItem)item.Clone());
                  }
  
                  //for (i = 0; i &lt; itens.Count; i ++)
                  //{                    
                  //    ListViewItem item = new ListViewItem((itens[i] as ListViewItem).Text);
  
                  //    for (j = 1; j &lt; (itens[i] as ListViewItem).SubItems.Count; j++)
                  //    {
                  //        item.SubItems.Add((itens[i] as ListViewItem).SubItems[j].Text);                     
                  //    }
                  //    listView1.Items.Add(item);
                  //}
  
                  for (i = 0; i &lt; listView1.Items.Count; i++)
                  {
                      listView1.Items[i].Text = "Passe " + (i + 1).ToString();
                  }
  
                  ////ListView listView = new ListView();
  
                  ////int j = 0;
  
                  ////for (i = 0; i &lt; listView1.Items.Count; i++)
                  ////{
                  ////    //listView.Items.Add(listView1.Items[i]);
                  ////    for (j = 0; j &lt; listView1.Items[i].SubItems.Count; j++)
                  ////    {
                  ////        listView.Items[i].SubItems.Add(listView1.Items[i].SubItems[j].Text);
                  ////    }
                  ////}
  
                  ////listView1.Items.Clear();
  
                  ////for (i = 0; i &lt; listView.Items.Count; i++)
                  ////{
                  ////    listView1.Items.Add(listView.Items[i].Clone() as ListViewItem);
                  ////}
  
                  //foreach (ListViewItem item in listViewCollection)
                  //{
                  //    listView1.Items.Add(item.Clone() as ListViewItem);
                  //}
  
                  SetWindowTheme(listView1.Handle, "Explorer", null);
                  limpando = false;
              }
  
             // SetWindowTheme(listView1.Handle, "Explorer", null);
          }
  </code></pre>