したがって、整数を入力して時間を形成するためのテキストボックスを備えたフォームがあり、理想的な作業プログラムは、基本的に、ユーザーが整数を入力してProcess
ボタンをクリックする前に、リストボックスからランナーを選択する必要があることを意味しますが、そうしない場合ランナーを選択してクリックProcess
すると、次のNullReferenceException
行でエラーがスローされます。
lblRunnerInfo.Text = lstRunners.SelectedItem.ToString() + "\r\n" + "Finished?: " + "\r\n" + "Time: " + txtHours.Text + ":" + txtMinutes.Text + ":" + txtSeconds.Text;
ボタンの完全なコードは次のとおりです。
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;
}
私が見逃している本当に単純なものがあるかもしれませんが、私はこれまでに経験したことがないNullReferenceException
ので、どんな助けも素晴らしいでしょう.