解决NullReferenceException未处理 [英] To resolve NullReferenceException was unhandled

查看:154
本文介绍了解决NullReferenceException未处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在以下代码段中收到NullReferenceException,

MyFile.xaml.cs

....

Hi All,

I am getting NullReferenceException on the below code snippet,

MyFile.xaml.cs

....

<combobox grid.column="1" name="myCombobox" selectedindex="0" horizontalalignment="Right" verticalalignment="left">
</combobox>


.....



.....


MyFile.cs

private void MyFunction(object sender, RoutedEventArgs e)
{
  string firstval = "";
  if(MyCheckbox.IsChecked == false)
  {
   ....
  }
  firstval = myCombobox.Text.ToString(); //here NullReferenceException occurs 
  else
  firstval = myCombobox.Text.Substring(5);
}


.....

解决上述问题的任何帮助将非常可观.
我正在使用VS2008 IDE,Win 7 SP1 o/s.

问候,
Samanth_90


.....

Any Help in resolving the above would be much appreciable.
I am using VS2008 IDE, Win 7 SP1 o/s.

With Regards,
Samanth_90

推荐答案

请注意,
firstval = myCombobox.Text.Substring(5);


如果文本框中的文本长度小于5,将引发异常.另外,其他情况也很正确,因为您不需要文本框中的ToString() 带有Text 属性


will throw an exception if the length of the text in the text box is less than 5. Also the others are right in that you do not need a ToString() with Text property from a text box


首先检查myCombobox.Text是否为空.
其次,您不必做myCombobox.Text.ToString(),myCombobox.Text会返回字符串本身.
First check whether myCombobox.Text is null or not.
Secondly, you don''t have to do myCombobox.Text.ToString(), myCombobox.Text returns string itself.


我猜
myCombobox.Text

为空值.
你能帮我确认一下吗?

is null value.
Can you confirm that for me?


这篇关于解决NullReferenceException未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆