質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Null

Nullとは、プログラミング言語やデータベースにおけるデータ表現の一種です。コンテキストによって"空"もしくは"長さ0の文字列"、”未知・不明”を意味します。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

1回答

35356閲覧

エラー「'オブジェクト参照がオブジェクト インスタンスに設定されていません。'」の解決方法を教えてください

aga-aga

総合スコア10

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Null

Nullとは、プログラミング言語やデータベースにおけるデータ表現の一種です。コンテキストによって"空"もしくは"長さ0の文字列"、”未知・不明”を意味します。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2019/02/23 09:46

編集2022/01/12 10:55

前提・実現したいこと

Visual Studio、C#、WPFの環境での、「System.NullReferenceException: 'オブジェクト参照がオブジェクト インスタンスに設定されていません。'」のエラーについて、解決方法を教えて下さい。

C#とWPFの勉強のために、下記のサイトを参考に、
簡単なWindows GUIプログラムを作っていました。

下記のサイトの「ダイアログ動作の確認」の項目まで行き、
プログラムを実行して「ダイアログ1」を押したところ、
サイトに示されているような画面にならず、エラーが発生してしまいました。

参考サイト
https://qiita.com/Kosen-amai/items/f2451acb2addd221193f

発生している問題・エラーメッセージ

エラー内容は以下のようでした。  System.NullReferenceException: 'オブジェクト参照がオブジェクト インスタンスに設定されていません。'  window.clickButtonText が null でした。 エラー箇所は「MainWindow.xaml.csファイル」内の34行目の部分です。    window.clickButtonText.Text = (sender as Button).Content + "が押されました。";

該当のソースコード

エラーが起きているファイル

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; namespace WpfApp20_19_02_23_A05 { /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void dialogButton_Click(object sender, RoutedEventArgs e) { // ウィンドウ生成 var window = new DialogWindow1(); // 生成したウィンドウのテキストに文字列設定 (sender as Button).Content + window.clickButtonText.Text = (sender as Button).Content + "が押されました。"; // ウィンドウ表示 bool? res = window.ShowDialog(); // DialogResultをチェック if (res == true) { textBlock.Text = "OKでダイアログが終了。"; } else { textBlock.Text = "キャンセルでダイアログが終了。"; } } } }

エラーの原因となっているであろうファイル

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.Shapes; namespace WpfApp20_19_02_23_A05 { /// <summary> /// DialogWindow1.xaml の相互作用ロジック /// </summary> public partial class DialogWindow1 : Window { // OK時 private void Button_Click(object sender, RoutedEventArgs e) { this.DialogResult = true; } // キャンセル時 private void Button_Click_1(object sender, RoutedEventArgs e) { this.DialogResult = false; } } }

表示されない画面のXAMLファイル

<Window x:Class="WpfApp20_19_02_23_A05.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:WpfApp20_19_02_23_A05" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="47*"/> <RowDefinition Height="58*"/> <RowDefinition Height="315*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="396*"/> <ColumnDefinition Width="397*"/> </Grid.ColumnDefinitions> <Button x:Name="button" Content="ダイアログ1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="150" Height="30" Click="dialogButton_Click"/> <Button x:Name="button1" Content="ダイアログ2" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="150" Height="30" Click="dialogButton_Click"/> <Button x:Name="button2" Content="次の画面に移動" HorizontalAlignment="Center" Grid.Row="2" VerticalAlignment="Center" Width="150" Height="30"/> <Button x:Name="button3" Content="新しい画面表示" Grid.Column="1" HorizontalAlignment="Center" Grid.Row="2" VerticalAlignment="Center" Width="150" Height="30"/> <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="174,27.667,0,0" Grid.Row="1" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/> </Grid> </Window>

「DialogWindow1」のxamlファイルです。

<Window x:Class="WpfApp20_19_02_23_A05.DialogWindow1" 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:WpfApp20_19_02_23_A05" mc:Ignorable="d" Title="DialogWindow1" Height="150" Width="300" WindowStyle="ToolWindow"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <TextBlock x:Name="clickButtonText" Text="TextBlock11" Margin="10"/> <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right"> <Button Content="OK" Width="80" Margin="0,10,10,10" IsDefault="True" Click="Button_Click"/> <Button Content="キャンセル" Width="80" Margin="0,10,10,10" IsCancel="True" Click="Button_Click_1"/> </StackPanel> </Grid> </Window>

補足情報(FW/ツールのバージョンなど)

XAMLで記述するファイルも含め、
プロジェクトのコード内容はサイトとほぼ同じです。

初心者すぎる質問かと思いますが、どうしたら良いのか分からないので、
教えてくださればと思います。

分かる方がいらっしゃいましたら、宜しくお願い致します。

開発環境

 Windows 10
Visual Studio Community 2017
C#
WPF


追記
回答して下さったお二方、どうもありがとうございます!
「DialogWindow1」のxamlファイルを追加しました。
どうぞよろしくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ozwk

2019/02/23 10:04 編集

DialogWindow1のxamlを貼って下さい
guest

回答1

0

DialogWindow1 の clickButtonText が定義されているけど値が null になっています。
ご提示のコードからは、DialogWindow1 の clickButtonText がなんであるか分かりません。
これ以上は clickButtonText に関わるコードをUPしてください。
--- コード追記 ---
MainWindow から DialogWindow1 のコントロールを直接触るのはお勧めできません。
以下は、DialogWindow1にMyMessageっていうプロパティを生やしてclickButtonText.Textの変更を行うサンプルです。
DialogWindow1のコードビハインドに書いてください。

C#

1 public static readonly DependencyProperty MyMessageProperty = 2 DependencyProperty.Register( 3 "MyMessage", 4 typeof(string), 5 typeof(Window), 6 new PropertyMetadata(null, MyMessageChanged)); 7 8 public object MyMessage 9 { 10 get 11 { 12 return GetValue(MyMessageProperty); 13 } 14 set 15 { 16 SetValue(MyMessageProperty, value); 17 } 18 } 19 20 private static void MyMessageChanged(DependencyObject d, 21 DependencyPropertyChangedEventArgs e) 22 { 23 clickButtonText.Text = e.NewValue as string; 24 }

投稿2019/02/23 10:00

編集2019/02/25 03:01
hihijiji

総合スコア4150

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問