Visual Studio / WPF > Error: System.NullReferenceException: 'オブジェクト参照がオブジェクトインスタンスに設定されていません。'

```txt:動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2
```

## Error

http://gushwell.ldblog.jp/archives/52300866.html
のサンプルを写経にて試そうとしていた。

確認ボタンを押下したところ、以下の部分において

```
 private void buttonConfirm_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(string.Format("{0} {1}", _company.Name, _company.WebSite));
        }
```

以下のエラーが出た。

> 例外はハンドルされていません
> System.NullReferenceException: 'オブジェクト参照がオブジェクトインスタンスに設定されていません。'

_companyがnullになっている。


どこか写し間違いがあるのかもしれない。
原因を特定しにくいエラーメッセージだ。慣れるしかなさそう。

### code

```MainWindoxw.xaml.cs
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 System.ComponentModel;

namespace _170605_t1025_INotifyPropertyChanged
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            _company = DataContext as Company;
        }
        private Company _company;

        private void buttonConfirm_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(string.Format("{0} {1}", _company.Name, _company.WebSite));
        }

        private void buttonChange_Click(object sender, RoutedEventArgs e)
        {
            _company.Name = "Microsoft";
            _company.WebSite = "http://www.bing.com";
        }
    }

    public class Company : INotifyPropertyChanged
    {
        private string name;
        private string webSite;
        public string Name {
            get { return this.name; }
            set
            {
                if (value != this.name)
                {
                    this.name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }
        public string WebSite
        {
            get { return this.webSite; }
            set
            {
                if (value != this.webSite)
                {
                    this.webSite = value;
                    NotifyPropertyChanged("WebSite");
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}
```

```MainWindow.xaml
<Window x:Class="_170605_t1025_INotifyPropertyChanged.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170605_t1025_INotifyPropertyChanged"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:Company x:Key="company" Name="Google" WebSite="http://www.google.com"/>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBox Name="textBox1" Text="{Binding Name, Mode=TwoWay}"/>
            <TextBox Name="textBox2" Text="{Binding WebSite, Mode=TwoWay}"/>
            <Button Content="確認" Height="23" x:Name="buttonConfirm" Width="75"
                    Click="buttonConfirm_Click"/>
            <Button Content="変更" Height="23" x:Name="buttonChange" Width="75"
                    Click="buttonChange_Click"/>
        </StackPanel>
    </Grid>
</Window>
```

## ミス

https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1170370711
> インスタンスが設定されていない、オブジェクト参照を使用するとよく出る例外なのね。

```
_company = DataContext as Company;
```

として受け取るDataContextの宣言をXAMLに書き忘れていた。

```
DataContext="{DynamicResource company}"
```

### 修正済code

```MainWindow.xaml.cs
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 System.ComponentModel;

namespace _170605_t1025_INotifyPropertyChanged
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            _company = DataContext as Company;
        }
        private Company _company;

        private void buttonConfirm_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(string.Format("{0} {1}", _company.Name, _company.WebSite));
        }

        private void buttonChange_Click(object sender, RoutedEventArgs e)
        {
            _company.Name = "Microsoft";
            _company.WebSite = "http://www.bing.com";
        }
    }

    public class Company : INotifyPropertyChanged
    {
        private string name;
        private string webSite;
        public string Name {
            get { return this.name; }
            set
            {
                if (value != this.name)
                {
                    this.name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }
        public string WebSite
        {
            get { return this.webSite; }
            set
            {
                if (value != this.webSite)
                {
                    this.webSite = value;
                    NotifyPropertyChanged("WebSite");
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }

}
```

```MainWindow.xaml
<Window x:Class="_170605_t1025_INotifyPropertyChanged.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170605_t1025_INotifyPropertyChanged"
        mc:Ignorable="d"
        DataContext="{DynamicResource company}"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:Company x:Key="company" Name="Google" WebSite="http://www.google.com"/>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBox Name="textBox1" Text="{Binding Name, Mode=TwoWay}"/>
            <TextBox Name="textBox2" Text="{Binding WebSite, Mode=TwoWay}"/>
            <Button Content="確認" Height="23" x:Name="buttonConfirm" Width="75"
                    Click="buttonConfirm_Click"/>
            <Button Content="変更" Height="23" x:Name="buttonChange" Width="75"
                    Click="buttonChange_Click"/>
        </StackPanel>
    </Grid>
</Window>
```