Erick Gallani
Erick Gallani

Reputation: 809

WPF getting class instance null reference on XAML

I'm getting null reference on trying to set up a class instance to my XAML.

XAML Code

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:classes="clr-namespace:WpfApplication1.Classes"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <classes:Person x:Key="person" /> <!-- here getting null reference -->
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Blue" />
            <Setter Property="FontSize" Value="30" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="FontFamily" Value="Arial" />
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="Background" Value="LightGray" />
        </Style>
        <Style TargetType="Button">
            <Setter Property="Foreground" Value="DodgerBlue" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Background" Value="White" />
        </Style>
    </Window.Resources>

    <StackPanel x:Name="stackPanelPerson" DataContext="{Binding Source={ StaticResource person }}">
        <TextBlock Text="Write your name" />
        <TextBox x:Name="boxUserName" Text="{Binding Name, Mode=TwoWay}"
                 Margin="0 20 0 0" />
        <TextBox x:Name="boxLastName" Text="{Binding LastName, Mode=TwoWay}"
                 Margin="0 20 0 0" />
        <TextBox x:Name="boxAge" Text="{Binding Age, Mode=TwoWay}"
                 Margin="0 20 0 0" />
        <Button x:Name="btnSayHello"
                Click="btnSayHello_Click"
                Margin="0 20 0 0">Say Hello</Button>
    </StackPanel>
</Window>

Here my class code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApplication1.Classes
{
    public class Person : INotifyPropertyChanged
    {
        public Person()
        {
            this.Name = "Name";
            this.LastName = "LastName";
            this.Age = 22;
        }

        private String name;

        public String Name
        {
            get { return name; }
            set 
            { 
                name = value;
                OnPropertyChanged("Name");
                OnPropertyChanged("FullName");
            }
        }

        private String lastName;

        public String LastName
        {
            get { return lastName; }
            set 
            { 
                lastName = value;
                OnPropertyChanged("LastName");
                OnPropertyChanged("FullName");
            }
        }

        private int age;

        public int Age
        {
            get { return age; }
            set 
            { 
                age = value;
                OnPropertyChanged("Age");
            }
        }

        private String fullName;

        public String FullName
        {
            get { return string.Format("{0} {1}", Name, LastName); }
            set 
            { 
                fullName = value;
                OnPropertyChanged("FullName");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string PropertyName)
        {
            if (PropertyName != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
    }
}

And here main window back code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApplication1.Classes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Person person;

        public MainWindow()
        {
            InitializeComponent();
            person = new Person();
        }

        private void btnSayHello_Click(object sender, RoutedEventArgs e)
        {
            string message = string.Format(" Hello {0} {1}, you told me that you are {2} years old. ", person.Name, person.LastName, person.Age);

            MessageBox.Show(message);
        }
    }
}

I dont know why I'm getting this null reference, because the class is been create on the MainWindow() method.

Anyone have a clue?

Thank you guys.

Upvotes: 1

Views: 957

Answers (1)

SLaks
SLaks

Reputation: 887215

As you'll see if you look at the exception stack trace, PropertyChanged is null. Checking if (PropertyName != null) doesn't prevent that.

Upvotes: 3

Related Questions