-
Nullreference exception
hi all
i have errors while bulding the code in c#
Code:
private void subMenuModify_Click(object sender, System.EventArgs e)
{
int nStart = X;
int spos = 0;
int epos = this.lvFiles.Columns[0].Width;
for (int i = 0; i < this.lvFiles.Columns.Count; i++)
{
if (nStart > spos && nStart < epos)
{
subItemSelected = i;
break;
}
spos = epos;
epos += this.lvFiles.Columns[i].Width;
}
subItemText = this.lvItem.SubItems[2].Text;
the line at which exception occurs is subItemText = this.lvItem.SubItems[2].Text;
the exception is as follows:
Nullreference exception was unhandled
Object reference not set to an instance of an object.
Pls anyone help me resolve this issue?
-
Looks like that item doesn't exist in your menu. Make sure there are at least 3 items in your menu or that will not work.
-
which item do you think is missing here? u mean "modify item"?
-
Code:
subItemText = this.lvItem.SubItems[2].Text;
This indexer tries to access something that doesn't exist probably. Make sure you have 3 items at least in this collection. It's usually not good to address menu's in this order unless you are guaranteed to have something there, otherwise you can get exceptions like this.