I am using WinForms/VS2010 c# to create a listview with 12 columns and 8 rows. Here is the snippet from the WinForms auto-coded file:
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
.....
}
Then, in the Form1.cs file, I have the following method:
// in my form1.cs file
public void listView1_Populate()
{
var columnIndex = listView1.Columns[2].Index;
int counter = 0;
foreach (ListViewItem item in listView1.Items)
{
if (item.SubItems[columnIndex].Text == "2")
{
counter += 100;
item.SubItems[columnIndex].Text = counter.ToString();
}
}
}
But I get the Object reference not set to an instance of an object exception when the code hits the line: var columnIndex = listView1.Columns[2].Index;
What am I doing wrong (other than trying to learn c# at age 54!)?
Its not clear from your question what is null referenced. But if you add the following before this line
var columnIndex = listView1.Columns[2].Index;you might get a clearer understanding of whats going wrong.