0

I'm Currently Working on a windows 8.1 Application and im trying to set the value of a textblock that is inside of a Pivot Page. When I try to set the value of the text Block I get a weird error about a Null Refrence Exception.

The Code for the XAML is as follows

<TextBlock x:Name="scoreFinal" Text="0" HorizontalAlignment="Left" Margin="235,408,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="34" Width="97" FontSize="32"/>

Im using an event Handler for TextChanged in a textbox to change the value of the TextBlock using the following code

private void score_TextChanged(object sender, TextChangedEventArgs e)
    {
        int totalPar=38;
        int actual=0;


        // actual = int.Parse(score1.Text) + int.Parse(score2.Text) + int.Parse(score3.Text) + int.Parse(score4.Text) + int.Parse(score5.Text) + int.Parse(score6.Text) + int.Parse(score7.Text) + int.Parse(score8.Text) + int.Parse(score9.Text);
        if (actual < totalPar)
        {


            scoreFinal.Text = ("-" + (totalPar - actual));

        }

When I run the page it loads fine and I have values on all my text boxes to be 0

but when i run this and edit text I get the following error

enter image description here

Anyone got a clue?

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
Hunter
  • 16
  • 4
  • Set a breakpoint on that line, then examine variables to see what is `null`. – John Saunders May 05 '15 at 03:07
  • Welcome to Stack Overflow! Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 05 '15 at 03:09
  • John You lead me into the right direction. I knew what as NullRefrence was but I did not know why it was doing that. According to the documentation I read I was believing that because it was in the XAML it must be instantiated. Apparently That is not the case. I don't know why I thought that but I did. So checking if it was null and then just creating it did seem to kind of help but I think by Creating it the way I did isn't quite right since Its creating a new text Block. – Hunter May 05 '15 at 03:40
  • I don't know WPF, but I wonder if the `scoreFinal` you reference in the `TextChanged` event is the same one as in the XAML. I think it is not. I have no idea how to reference controls which are inside of other controls in WPF, unless it's something like `pivotPanel.scoreFinal`? – John Saunders May 05 '15 at 03:59
  • I think It has to do with the XAML not creating the object just declaring it in a way. Because if i use similar code on a page with less items its fine. Maybe it has to do with how long it take for the page to load? Thanks for your insight though. – Hunter May 05 '15 at 04:10
  • This is very strange it should work.. is your scoreFinal textbox in other pivot item. can you share you pivot xaml? – Muhammad Saifullah May 05 '15 at 07:16

2 Answers2

0

There is a chance for scoreFinal control is not initialized when score_TextChanged event fired .

Try to hookup TextChanged in page loaded event .

   void YourPage_Loaded(object sender, RoutedEventArgs e)
        {
         score2.TextChanged+=score_TextChanged;

        }

Also don't forget to remove the event handler from XAML.

Null Pointer
  • 9,089
  • 26
  • 73
  • 118
-2

In your XAML Code There is no TextChanged Event. I hope it doesn't created Well.

<TextBlock x:Name="scoreFinal" Text="0" HorizontalAlignment="Left" Margin="235,408,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="34" Width="97" FontSize="32" TextChanged="score_TextChanged"/>
Dino
  • 199
  • 2
  • 13
  • There is a textBox with the TextChanged Event Handler ' ' – Hunter May 05 '15 at 03:16
  • His XAML is for a different control. – Nacimota May 05 '15 at 03:21
  • I create an sample pivot application and put my code(Mention in my answer) after Block. If I press any key it changed to -38. I don't know what's wrong your code. If you ask I will give the whole code. And Where you Pleased your xaml code? – Dino May 05 '15 at 04:12