Control order causes "System.Nullreferenceexception"

Asked

Viewed 111 times

5

I’m doing some initial tests with the WPF and I came up with this question.

Make no mistake:

<DockPanel Margin="5">
    <TextBox x:Name="myTextBox" DockPanel.Dock="Right" Width="50" />
    <Slider x:Name="mySlider" Width="300" SmallChange="1" TickPlacement="BottomRight" IsSnapToTickEnabled="True" ValueChanged="mySlider_ValueChanged" Minimum="1" />
</DockPanel>

private void mySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) 
{ 
    myTextBox.Text = (Convert.ToInt32(mySlider.Value)).ToString(); 
} 

On the other hand, changing the order of the controls get:

An Exception of type 'System.Nullreferenceexception' occurred in mySlider_error.exe but was not handled in user code

Makes a mistake:

    <DockPanel Margin="5">
        <Slider x:Name="mySlider" Width="300" SmallChange="1" TickPlacement="BottomRight" IsSnapToTickEnabled="True" ValueChanged="mySlider_ValueChanged" Minimum="1" />
        <TextBox x:Name="myTextBox" DockPanel.Dock="Right" Width="50" />
    </DockPanel>

I know I can easily solve this scenario using, for example:

<TextBox x:Name="myTextBox" DockPanel.Dock="Right" Width="50" Text="{Binding ElementName=mySlider,Path=Value}" />

I only wish to know why the exception is made?

  • 1

    Post the code of: mySlider_ValueChanged

  • 3

    My colleague @rubStackOverflow is absolutely right: post the code! My supposition is that this code, being an event handler, is executed at least once when the object mySlider is created. If that code has/references to the object myTextBox, the order of creation certainly matters.

  • Yes refers to myTextbox.

  • what’s on your mySlider_ValueChanged?

  • private void mySlider_ValueChanged(Object Sender, Routedpropertychangedeventargs<double> e) { myTextBox.Text = (Convert.Toint32(mySlider.Value). Tostring(); }

  • Have you seen what you’re going through (Convert.ToInt32(mySlider.Value)).ToString(); so that you convert to int and then to string?

  • Do not have to convert to string to save in Text?

Show 2 more comments

2 answers

1

I ended up settling it this way:

    private void atualizarValores_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (!(textBoxProduto == null))
            textBoxProduto.Text = (int.Parse(textBoxMultiplicador.Text) * int.Parse(textBoxMultiplicando.Text)).ToString();
    }

0

During the construction of the Slider component on the screen load its value is set to the default value, when doing this, the mySlider_ValueChanged event is triggered and as myTextBox has not yet been built it is null.

A solution would be something like this:

    private bool carregamentoCompleto = false;

    private void mySlider_ValueChanged(object sender,RoutedPropertyChangedEventArgs<double> e)
    {
        if(carregamentoCompleto)
        {
            myTextBox.Text = (Convert.ToInt32(mySlider.Value)).ToString();
        }
    }

    private void MeuWindows_Loaded(object sender, RoutedEventArgs e)
    {
        carregamentoCompleto = true;
        myTextBox.Text = (Convert.ToInt32(mySlider.Value)).ToString();
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.