I am getting and error on this line:
String selectedValue = this.employeeList.SelectedValue.ToString();
The error I am getting is
NullReferenceException was unhandled.
What does this mean? Can someone help me understand why I am getting this error. Down below is the whole code.
String filePath = this.txtFilePath.Text;
if (!String.IsNullOrEmpty(filePath))
{
MessageBox.Show("No file path specified");
}
if (this.employeeList.SelectedIndex != -1)
{
String selectedValue = this.employeeList.SelectedValue.ToString();
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine(selectedValue);
}
}
else
{
MessageBox.Show("No item selected");
}
That means you are trying to access something which is null (not initialized to any valid value). So add a null check before accessing that.
I guess in this case, the
employeeList.SelectedValueis probably null.If you run into any such errors , Always use Visual Studio Breakpoints and Step thru line by line to see what values are coming and where it is breaking. That is the best way to understand what is wrong with the code.