0
votes

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?

2
Set a breakpoint on that line, then examine variables to see what is null.John Saunders
Welcome to Stack Overflow! Almost all cases of NullReferenceException are the same. Please see "What is a NullReferenceException and how do I fix it?" for some hints.John Saunders
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
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
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

2 Answers

0
votes

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.

-2
votes

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"/>