0

所以我有一个带有文本框的表单来输入整数以形成时间,理想的工作程序基本上意味着用户必须先从列表框中选择一个跑步者,然后才能输入任何整数并单击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过,所以任何帮助都会很棒。

4

3 回答 3

3

替换此语句

if (lstRunners.SelectedIndex > -1)

有了这个:

if (lstRunners.SelectedItems.Count > 0)
于 2013-11-01T19:30:20.263 回答
2

NullReferenceException当您尝试访问为空的对象时会发生A。例如,调用myString.ToString()when myStringis null 会导致 ex。

在您的代码中:

lblRunnerInfo.Text = lstRunners.SelectedItem.ToString() + "\r\n" + "Finished?: " + "\r\n" + "Time: " + txtHours.Text + ":" + txtMinutes.Text + ":" + txtSeconds.Text;

这些项目似乎是设计者添加的控件。调用Text此类控件的属性非常好,因为它们是由设计器代码初始化的。问题在于Selector.SelectedItem。从 MSDN 我们知道这个属性可以返回 null:

Gets or sets the first item in the current selection or returns null if the selection is empty.  

所以你需要执行一个空检查:

        string selectedItemText = "";
        string newline = Environment.NewLine;
        if(lstRunners.SelectedItem != null)
        {
           selectedItemText = lstRunners.SelectedItem.ToString() 
        }            
        string result = String.Format("{0}{1}Finished?: {1} Time: {3}:{4}:{5}",selectedItemText, newline,txtHours.Text,txtMinutes.Text,txtSeconds.Text);
        lblRunnerInfo.Text = result;
于 2013-11-01T19:41:44.063 回答
1

最有可能的罪魁祸首是它lstRunners.SelectedItemnull. 尝试添加条件检查null,如下所示:

if (lstRunners.SelectedItem != null)
{
   // 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;
}

更多关于 NRE 的信息在这里这里这里

于 2013-11-01T19:29:41.463 回答