I have a few buttons that send text string data from a textbox to a textblock on another page. Please see code below.
private void button_Click(object sender, RoutedEventArgs e)
{
Application.Current.Properties["obj1"] = textbox.Text;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Application.Current.Properties["obj2"] = textbox2.Text;
}
An exception of type 'System.NullReferenceException' occurred in
WpfApplication4.exe but was not handled in user code
Additional information: Object reference not set to an instance of an
object.
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
textBlock.Text = Application.Current.Properties["obj1"].ToString();
textBlock2.Text = Application.Current.Properties["obj2"].ToString();
}
The issue that Application.Current.Properties["obj1"]
please use null propagation ?.
to be sure that in case property is not set you will not try to call ToString()
for it
textBlock.Text = Application.Current.Properties["obj1"]?.ToString();