Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>

.....


C#
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);
}

.....

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

With Regards,
Samanth_90
Posted
Updated 11-Apr-12 2:41am
v2
Comments
[no name] 11-Apr-12 9:02am    
Why are you calling ToString on a string? That makes no sense.
What on that line is null?

Note that
firstval = myCombobox.Text.Substring(5);

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
 
Share this answer
 
First check whether myCombobox.Text is null or not.
Secondly, you don't have to do myCombobox.Text.ToString(), myCombobox.Text returns string itself.
 
Share this answer
 
I guess
myCombobox.Text
is null value.
Can you confirm that for me?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900