Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NullReferenceException未处理的修复程序?_C#_Winforms - Fatal编程技术网

C# NullReferenceException未处理的修复程序?

C# NullReferenceException未处理的修复程序?,c#,winforms,C#,Winforms,因此,我有一个带有文本框的表单,用于输入整数以形成一个时间,理想的工作程序基本上意味着用户必须从列表框中选择一个运行程序,然后才能输入任何整数并单击处理按钮,但是,当我没有选择一个运行程序并单击Process时,会为此行抛出NullReferenceException错误: lblRunnerInfo.Text=lstranners.SelectedItem.ToString()+“\r\n”+“完成”:“+”\r\n“+”时间:“+txtHours.Text+”:“+txtMinutes.Te

因此,我有一个带有文本框的表单,用于输入整数以形成一个时间,理想的工作程序基本上意味着用户必须从列表框中选择一个运行程序,然后才能输入任何整数并单击
处理
按钮,但是,当我没有选择一个运行程序并单击
Process
时,会为此行抛出
NullReferenceException
错误:

lblRunnerInfo.Text=lstranners.SelectedItem.ToString()+“\r\n”+“完成”:“+”\r\n“+”时间:“+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
,因此任何帮助都会很好。

最有可能的罪魁祸首是
lstRunner。SelectedItem
将以
null
的形式返回。尝试为
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的信息,以及。

最有可能的罪魁祸首是
lstreans。SelectedItem
返回为
null
。尝试为
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的更多信息,以及。

替换此声明

if (lstRunners.SelectedIndex > -1)
为此:

if (lstRunners.SelectedItems.Count > 0)
替换此语句

if (lstRunners.SelectedIndex > -1)
为此:

if (lstRunners.SelectedItems.Count > 0)

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

在代码中:

lblRunnerInfo.Text = lstRunners.SelectedItem.ToString() + "\r\n" + "Finished?: " + "\r\n" + "Time: " + txtHours.Text + ":" + txtMinutes.Text + ":" + txtSeconds.Text;
这些项似乎是设计器添加的控件。对此类控件调用
Text
属性非常好,因为它们是由设计器代码初始化的。问题出在你身上。从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;

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

在代码中:

lblRunnerInfo.Text = lstRunners.SelectedItem.ToString() + "\r\n" + "Finished?: " + "\r\n" + "Time: " + txtHours.Text + ":" + txtMinutes.Text + ":" + txtSeconds.Text;
这些项似乎是设计器添加的控件。对此类控件调用
Text
属性非常好,因为它们是由设计器代码初始化的。问题出在你身上。从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;

“我以前从未遇到过
NullReferenceException
,所以任何帮助都很好。”第一个项目?调试这些是每个.NET开发人员可能比他们希望的更经常遇到的事情。只要在那一行上放一个断点,看看什么是空的。我猜是
lstreans。选择editem
。然后做一些事情来修复它:当有人点击流程而没有选择一个跑步者时会发生什么?他们甚至应该被允许这样做,或者你应该禁用这个按钮吗?是的,我希望这个按钮被禁用,直到用户选择一个跑步者。阅读这个。这将对您有所帮助。您应该附加到
lstreans
的事件以启用和禁用您的按钮。谢谢,让它工作了!“我以前从未遇到过
NullReferenceException
,所以任何帮助都很好。”第一个项目?调试这些是每个.NET开发人员可能比他们希望的更经常遇到的事情。只要在那一行上放一个断点,看看什么是空的。我猜是
lstreans。选择editem
。然后做一些事情来修复它:当有人点击流程而没有选择一个跑步者时会发生什么?他们甚至应该被允许这样做,或者你应该禁用这个按钮吗?是的,我希望这个按钮被禁用,直到用户选择一个跑步者。阅读这个。这将对您有所帮助。您应该附加到
lstreans
的事件以启用和禁用您的按钮。谢谢,让它工作了!if(lstranners.SelectedItems.Any())if(lstranners.SelectedItems.Any())