So I have a form with textboxes to input integers to form a time and the ideal working program basically means that the user has to select a runner from the listbox before they can type in any integers and click the Process
button, but when I don't select a runner and click Process
the NullReferenceException
error is thrown for this line:
lblRunnerInfo.Text = lstRunners.SelectedItem.ToString() + "\r\n" + "Finished?: " + "\r\n" + "Time: " + txtHours.Text + ":" + txtMinutes.Text + ":" + txtSeconds.Text;
The full code for the button is as follows:
private void btnProcess_Click(object sender, EventArgs e)
{
// Converts variables attached to textboxes to integers
hoursInt = Convert.ToInt32(txtHours.Text);
minutesInt = Convert.ToInt32(txtMinutes.Text);
secondsInt = Convert.ToInt32(txtSeconds.Text);
// Check if a runner has been selected
if (lstRunners.SelectedIndex > -1)
{
// Obtain selected runner
Runner selectedRunner = (Runner)lstRunners.SelectedItem;
// Call the method in Gate class to process the runner
gate.ProcessRunner(selectedRunner);
}
else
{
MessageBox.Show("Please select a runner!");
}
// Converts the total to a string and outputs it as a label
lblFinished.Text = gate.Total.ToString();
lblRunnerInfo.Text = lstRunners.SelectedItem.ToString() + "\r\n" + "Finished?: " + "\r\n" + "Time: " + txtHours.Text + ":" + txtMinutes.Text + ":" + txtSeconds.Text;
}
There may be something really simple I am missing, but I've never had a NullReferenceException
before, so any help would be great.