0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visual Studio | WPF > string > Error: System.NullReferenceException: 'オブジェクト参照がオブジェクト インスタンスに設定されていません。' > 初期値未指定時にnull

Last updated at Posted at 2017-12-05
動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

エラーの発生

code

MainWindow.xaml
<Window x:Class="_171205_t1530_propertyStringAdd.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:_171205_t1530_propertyStringAdd"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <Button Name="B_add" Content="Add" Height="30" Click="B_add_Click"/>
            <TextBox Name="T_Memo" VerticalScrollBarVisibility="Visible"
                     Height="250" Text="{Binding RecvString}"/>
        </StackPanel>
        
    </Grid>
</Window>
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;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace _171205_t1530_propertyStringAdd
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private string _recvString;

        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = this;
        }

        public string RecvString
        {
            get { return _recvString; }
            set
            {
                _recvString = value;
                OnPropertyChanged("RecvString");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName = null)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private void B_add_Click(object sender, RoutedEventArgs e)
        {
            if (RecvString.Length > 0)
            {
                RecvString += System.Environment.NewLine;
            }
            RecvString += "TEST";
        }
    }
}

エラーの内容

B_add_Click()の下記部分にてエラーが出る。

if (RecvString.Length > 0)
{

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

エラーの理由は、private string _recvString;としている部分で初期値が代入されていないことによる。

参考: 各型のデフォルト値についてのメモ (default, int, long, float, double) by いろいろ備忘録日記さん

string型の初期値未設定時はnullになり、そのLengthをとろうとしてエラーが出る。

対応

案1

private string _recvString = string.Empty;
として宣言する。

案2

Lengthなどを使う前にnullチェックを入れる。

if (RecvString != null && RecvString.Length > 0)
{
    RecvString += System.Environment.NewLine;
}

関連

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?