用户代码未处理NullReferanceException [英] NullReferanceException was unhandled by usercode

查看:64
本文介绍了用户代码未处理NullReferanceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hii

我陷入了nullreferanceexception错误的错误,用户代码未处理

i m stuck with the error of nullreferanceexception error was unhandled by user code

帮我解决这个问题.

xaml:

< Grid x:Name =" grid_AdminData"保证金="1,4,1,0">
                             < Grid.RowDefinitions>
                    < RowDefinition/>
                    < RowDefinition/>
                    < RowDefinition/>
                    < RowDefinition/>
                    < RowDefinition/>
                    < RowDefinition/>
                    < RowDefinition/>
                    < RowDefinition/>
                    < RowDefinition/>
                             </Grid.RowDefinitions>
                             < Grid.ColumnDefinitions>
                    < ColumnDefinition/>
                    < ColumnDefinition/>
                    < ColumnDefinition/>
                    < ColumnDefinition/>
                             </Grid.ColumnDefinitions>
                             < Label Content =" AdminID:"; Horizo​​ntalAlignment =左" VerticalAlignment =顶部"高度="18".填充="0,0,5,5".前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="82". FontSize ="14"保证金="1,0,0,0&"/>
                             <标签内容=城市:"; Target ="{Binding ElementName = txtCity}" Horizo​​ntalAlignment =左" VerticalAlignment =顶部"高度="18". 填充="0,0,5,0";前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="82". FontSize ="14" Grid.Column ="1". Grid.Row ="2".保证金="0,1,0,0&"/>
                             <标签内容=全名:" Target ="{Binding ElementName = txtFullName}" Horizo​​ntalAlignment =左" VerticalAlignment =顶部" 高度="18".填充="0,0,5,5".前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="82". FontSize ="14" Grid.Column ="1".保证金="0"/>
                             <标签内容=地址:" Horizo​​ntalAlignment =左" VerticalAlignment =顶部"高度="18".填充="0,0,5,5".前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="71".保证金="1,1,0,0"; Grid.Row ="2". FontSize ="14"/>
                             <标签内容=状态:"; Horizo​​ntalAlignment =左" VerticalAlignment =顶部"高度="18".填充="0,0,5,5".前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="82". FontSize ="14" Grid.Column ="1". Grid.Row ="4".保证金="2,1,0,0"/>
                             <标签内容=邮政编码:" Horizo​​ntalAlignment =左" VerticalAlignment =顶部"高度="18".填充="0,0,5,5".前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="93". FontSize ="14" Grid.Column ="1". Grid.Row ="6".保证金="0,1,0,0&"/>
                             <标签内容=性别:"; Horizo​​ntalAlignment =左" VerticalAlignment =顶部"高度="18".填充="0,0,5,5".前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="101". FontSize ="14" Grid.Column ="2".保证金="1,0,0,0&"/>
                             <标签内容=电子邮件ID:" Horizo​​ntalAlignment =左" VerticalAlignment =顶部"高度="18".填充="0,0,5,5".前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="82". FontSize ="14" Grid.Column ="2". Grid.Row ="2".保证金="1,1,0,0"/>
                             <标签内容=名称:"; Horizo​​ntalAlignment =左" VerticalAlignment =顶部"高度="18".填充="0,0,5,2".前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="101". FontSize ="14" Grid.Column ="2". Grid.Row ="4".保证金="1,1,0,0"/>
                             <标签内容=员工照片:" Horizo​​ntalAlignment =左" VerticalAlignment =顶部"高度="18".填充="0,0,5,2". 前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="118". FontSize ="14" Grid.Column ="3". Grid.Row ="6".保证金="1,1,0,0"/>
                             <标签内容="用户名:" Horizo​​ntalAlignment =左" VerticalAlignment =顶部"高度="18".填充="0,0,5,2".前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="101". FontSize ="14" Grid.Column ="3".保证金="1,0,0,0&"/>
                             <标签内容=密码:"; Horizo​​ntalAlignment =左" VerticalAlignment =顶部"高度="18".填充="0,0,5,2".前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="101". FontSize ="14" Grid.Column ="3". Grid.Row ="2".保证金="1,1,0,0"/>
                             < Label Content =" ConfirmPassword:" Horizo​​ntalAlignment =左" VerticalAlignment =顶部"高度="18".填充="0,0,5,2". 前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="129". FontSize ="14" Grid.Column ="3". Grid.Row ="4".保证金="1,1,0,0"/>
                             < TextBox x:Name =" txtID" Grid.Row ="1".保证金="0,0,45,0&".填充="0". FontStyle =普通" FontSize ="14" FontFamily =" Times 新罗马书》 FontWeight ="Normal". IsReadOnly ="True"/>
                             < TextBox x:Name =" txtFullName"样式="{StaticResource VallidationTemplate}" Validation.Error =" Validation_Error"
                   文本="{{Binding UpdateSourceTrigger = LostFocus,Path = FullName,ValidatesOnExceptions = True,
                    ValidatesOnDataErrors = true,NotifyOnValidationError = true}" Grid.Row ="1".保证金="0,1,40,0&". Grid.Column ="1". FontFamily =" Times 新罗马书》 FontSize ="14" FontStyle =普通" FontWeight ="Normal".身高="19"/
                             < TextBox x:Name =" txtUserName" Grid.Row ="1".保证金="0,0,40,0&"; Grid.Column ="3". FontFamily ="Times New Roman"; FontSize ="14" FontStyle =普通" FontWeight ="Normal". IsReadOnly ="True"/>
                             < TextBox x:Name =" txtDesignation" Grid.Row ="5".保证金="0,0,41,0&"; Grid.Column ="2". FontFamily ="Times New Roman"; FontSize ="14" FontStyle =普通" FontWeight =普通"/>
                             < TextBox x:Name =" txtEmpPhoto" Grid.Row ="7".保证金="0,0,100,0&"; Grid.Column ="3". FontFamily ="Times New Roman"; FontSize ="14" FontStyle =普通" FontWeight ="Normal". IsReadOnly ="True"/>
                             < RadioButton x:Name =" rbtMale"内容=男性" Grid.Column ="2". Grid.Row ="1". FontFamily ="Times New Roman"; FontSize ="14" FontStyle =普通"保证金="0,0,113,0&";前景=白色"/
                             < RadioButton x:Name ="rbtFemale" FontStyle =普通" FontFamily ="Times New Roman"; FontSize ="14" Grid.Column ="2".保证金="63,0,41,0&" Grid.Row ="1".内容=女性"前景=白色"/
                             < TextBox x:Name =" txtCity"样式="{StaticResource VallidationTemplate}" Validation.Error =" Validation_Error"
                   文本="{{Binding UpdateSourceTrigger = LostFocus,Path = City,ValidatesOnExceptions = True,
                    ValidatesOnDataErrors = true,NotifyOnValidationError = true}" Grid.Row ="3".保证金="0,1,40,0&". Grid.Column ="1". FontFamily =" Times 新罗马书》 FontSize ="14" FontStyle =普通" FontWeight ="Normal".身高="19"/
                             < TextBox x:Name =" txtState"样式="{StaticResource VallidationTemplate}" Validation.Error =" Validation_Error"
                   文字="{{Binding UpdateSourceTrigger = LostFocus,Path = State,ValidatesOnExceptions = True,
                    ValidatesOnDataErrors = true,NotifyOnValidationError = true}" Grid.Row ="5".保证金="0,1,40,18". Grid.Column ="1". FontFamily ="Times New Roman"; FontSize ="14" FontStyle =普通" FontWeight ="Normal".高度="20". Grid.RowSpan ="2"/>
                             < TextBox x:Name =" txtPostal"样式="{StaticResource VallidationTemplate}" Validation.Error =" Validation_Error"
                   文本="{{Binding UpdateSourceTrigger = LostFocus,Path = PostalCode,ValidatesOnExceptions = True,
                    ValidatesOnDataErrors = true,NotifyOnValidationError = true}" Grid.Row ="7".保证金="0,0,40,0&"; Grid.Column ="1". FontFamily =" Times 新罗马书》 FontSize ="14" FontStyle =普通" FontWeight =普通"/>
                             < TextBox x:Name =" txtAddress"样式="{StaticResource VallidationTemplate}" Validation.Error =" Validation_Error"
                   文字="{{Binding UpdateSourceTrigger = LostFocus,Path = Address,ValidatesOnExceptions = True,
                    ValidatesOnDataErrors = true,NotifyOnValidationError = true}" Grid.Row ="3".保证金="0,1,45,1". Grid.RowSpan ="5". FontFamily ="Times New Roman"; FontSize ="14" FontStyle =普通" FontWeight ="Normal".身高="97"/>
                             < TextBox x:Name =" txtEMail" Grid.Row ="3".保证金="0,0,41,0&"; Grid.Column ="2". FontSize ="14" FontFamily ="Times New" 罗马" FontStyle =普通" FontWeight =普通"/>
                             <标签内容=移动设备:"; Horizo​​ntalAlignment =左" VerticalAlignment =顶部"高度="18".填充="0,0,5,2".前景=白色". FontStyle =普通" FontWeight =粗体"; FontFamily ="Times New Roman";宽度="101". FontSize ="14" Grid.Column ="2". Grid.Row ="6".保证金="1,1,0,0"/>
                             < TextBox x:Name =" txtMobile" Grid.Row ="7".保证金="0,0,41,0&"; Grid.Column ="2". FontFamily ="Times New Roman"; FontSize ="14" FontStyle =普通" FontWeight =普通"/>
                             < PasswordBox Horizo​​ntalAlignment =左"; VerticalAlignment =顶部" Grid.Column ="3". Grid.Row ="3".宽度="133".高度="20"/
                             < PasswordBox x:Name =" txt" Horizo​​ntalAlignment =左" VerticalAlignment =顶部" Grid.Column ="3". Grid.Row ="5".宽度="133". 高度="20". FontWeight ="Normal". FontFamily ="Times New Roman"; FontSize ="14"/>
                             <按钮x:Name =" btnBrowse" Style ="{StaticResource BrowseButtonStyleTemplate}"内容=浏览" Horizo​​ntalAlignment =左" VerticalAlignment =顶部" 宽度="58". Grid.Column ="3". Grid.Row ="7".保证金="114,0,0,0&"; FontStyle =普通"填充="1,0,1,1".点击="btnBrowse_Click"; FontFamily ="Times New Roman";高度="20"/
           </Grid>

<Grid x:Name="grid_AdminData" Margin="1,4,1,0">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Label Content="AdminID :" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,5" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="82" FontSize="14" Margin="1,0,0,0"/>
                <Label Content="City :" Target="{Binding ElementName=txtCity}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,0" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="82" FontSize="14" Grid.Column="1" Grid.Row="2" Margin="0,1,0,0"/>
                <Label Content="Full Name :" Target="{Binding ElementName=txtFullName}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,5" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="82" FontSize="14" Grid.Column="1" Margin="0"/>
                <Label Content="Address :" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,5" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="71" Margin="1,1,0,0" Grid.Row="2" FontSize="14"/>
                <Label Content="State :" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,5" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="82" FontSize="14" Grid.Column="1" Grid.Row="4" Margin="2,1,0,0"/>
                <Label Content="Postal Code :" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,5" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="93" FontSize="14" Grid.Column="1" Grid.Row="6" Margin="0,1,0,0"/>
                <Label Content="Gender :" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,5" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="101" FontSize="14" Grid.Column="2" Margin="1,0,0,0"/>
                <Label Content="E-Mail ID :" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,5" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="82" FontSize="14" Grid.Column="2" Grid.Row="2" Margin="1,1,0,0"/>
                <Label Content="Designation :" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,2" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="101" FontSize="14" Grid.Column="2" Grid.Row="4" Margin="1,1,0,0"/>
                <Label Content="Employee Photo :" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,2" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="118" FontSize="14" Grid.Column="3" Grid.Row="6" Margin="1,1,0,0"/>
                <Label Content="UserName:" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,2" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="101" FontSize="14" Grid.Column="3" Margin="1,0,0,0"/>
                <Label Content="Password :" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,2" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="101" FontSize="14" Grid.Column="3" Grid.Row="2" Margin="1,1,0,0"/>
                <Label Content="ConfirmPassword :" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,2" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="129" FontSize="14" Grid.Column="3" Grid.Row="4" Margin="1,1,0,0"/>
                <TextBox x:Name="txtID" Grid.Row="1" Margin="0,0,45,0" Padding="0" FontStyle="Normal" FontSize="14" FontFamily="Times New Roman" FontWeight="Normal" IsReadOnly="True"/>
                <TextBox x:Name="txtFullName" Style="{StaticResource VallidationTemplate}" Validation.Error="Validation_Error"
                    Text="{Binding UpdateSourceTrigger=LostFocus, Path=FullName,ValidatesOnExceptions=True,
                    ValidatesOnDataErrors=true, NotifyOnValidationError=true}" Grid.Row="1" Margin="0,1,40,0" Grid.Column="1" FontFamily="Times New Roman" FontSize="14" FontStyle="Normal" FontWeight="Normal" Height="19"/>
                <TextBox x:Name="txtUserName" Grid.Row="1" Margin="0,0,40,0" Grid.Column="3" FontFamily="Times New Roman" FontSize="14" FontStyle="Normal" FontWeight="Normal" IsReadOnly="True"/>
                <TextBox x:Name="txtDesignation" Grid.Row="5" Margin="0,0,41,0" Grid.Column="2" FontFamily="Times New Roman" FontSize="14" FontStyle="Normal" FontWeight="Normal"/>
                <TextBox x:Name="txtEmpPhoto" Grid.Row="7" Margin="0,0,100,0" Grid.Column="3" FontFamily="Times New Roman" FontSize="14" FontStyle="Normal" FontWeight="Normal" IsReadOnly="True"/>
                <RadioButton x:Name="rbtMale" Content="Male" Grid.Column="2" Grid.Row="1" FontFamily="Times New Roman" FontSize="14" FontStyle="Normal" Margin="0,0,113,0" Foreground="White"/>
                <RadioButton x:Name="rbtFemale" FontStyle="Normal" FontFamily="Times New Roman" FontSize="14" Grid.Column="2" Margin="63,0,41,0" Grid.Row="1" Content="Female" Foreground="White"/>
                <TextBox x:Name="txtCity" Style="{StaticResource VallidationTemplate}" Validation.Error="Validation_Error"
                    Text="{Binding UpdateSourceTrigger=LostFocus, Path=City,ValidatesOnExceptions=True,
                    ValidatesOnDataErrors=true, NotifyOnValidationError=true}" Grid.Row="3" Margin="0,1,40,0" Grid.Column="1" FontFamily="Times New Roman" FontSize="14" FontStyle="Normal" FontWeight="Normal" Height="19"/>
                <TextBox x:Name="txtState" Style="{StaticResource VallidationTemplate}" Validation.Error="Validation_Error"
                    Text="{Binding UpdateSourceTrigger=LostFocus, Path=State,ValidatesOnExceptions=True,
                    ValidatesOnDataErrors=true, NotifyOnValidationError=true}" Grid.Row="5" Margin="0,1,40,18" Grid.Column="1" FontFamily="Times New Roman" FontSize="14" FontStyle="Normal" FontWeight="Normal" Height="20" Grid.RowSpan="2"/>
                <TextBox x:Name="txtPostal" Style="{StaticResource VallidationTemplate}" Validation.Error="Validation_Error"
                    Text="{Binding UpdateSourceTrigger=LostFocus, Path=PostalCode,ValidatesOnExceptions=True,
                    ValidatesOnDataErrors=true, NotifyOnValidationError=true}" Grid.Row="7" Margin="0,0,40,0" Grid.Column="1" FontFamily="Times New Roman" FontSize="14" FontStyle="Normal" FontWeight="Normal"/>
                <TextBox x:Name="txtAddress" Style="{StaticResource VallidationTemplate}" Validation.Error="Validation_Error"
                    Text="{Binding UpdateSourceTrigger=LostFocus, Path=Address,ValidatesOnExceptions=True,
                    ValidatesOnDataErrors=true, NotifyOnValidationError=true}" Grid.Row="3" Margin="0,1,45,1" Grid.RowSpan="5" FontFamily="Times New Roman" FontSize="14" FontStyle="Normal" FontWeight="Normal" Height="97"/>
                <TextBox x:Name="txtEMail" Grid.Row="3" Margin="0,0,41,0" Grid.Column="2" FontSize="14" FontFamily="Times New Roman" FontStyle="Normal" FontWeight="Normal"/>
                <Label Content="Mobile :" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Padding="0,0,5,2" Foreground="White" FontStyle="Normal" FontWeight="Bold" FontFamily="Times New Roman" Width="101" FontSize="14" Grid.Column="2" Grid.Row="6" Margin="1,1,0,0"/>
                <TextBox x:Name="txtMobile" Grid.Row="7" Margin="0,0,41,0" Grid.Column="2" FontFamily="Times New Roman" FontSize="14" FontStyle="Normal" FontWeight="Normal"/>
                <PasswordBox HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="3" Grid.Row="3" Width="133" Height="20"/>
                <PasswordBox x:Name="txt" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="3" Grid.Row="5" Width="133" Height="20" FontWeight="Normal" FontFamily="Times New Roman" FontSize="14"/>
                <Button x:Name="btnBrowse" Style="{StaticResource BrowseButtonStyleTemplate}" Content="Browse" HorizontalAlignment="Left" VerticalAlignment="Top" Width="58" Grid.Column="3" Grid.Row="7" Margin="114,0,0,0" FontStyle="Normal" Padding="1,0,1,1" Click="btnBrowse_Click" FontFamily="Times New Roman" Height="20"/>
            </Grid>

Cs:

命名空间ITA
{
    ///< summary>
    ///AdminMaster.xaml的交互逻辑
    ///</summary>
   公共局部类AdminMaster:Window
    {
       private int _errors = 0;
      私人Person _Person = new Person();

      公共AdminMaster()
       {
           InitializeComponent();
           grid_AdminData.DataContext = _Person;
       }

       public string conn_String =数据源= Dell;初始目录= ITA;用户ID = sa;密码= 12345";

      私有void btnBrowse_Click(对象发送者,RoutedEventArgs e)
       {
           OpenFileDialog ofd = new OpenFileDialog();
          空的< bool> dlgRes = ofd.ShowDialog();
          如果(dlgRes == true)
           {
                             txtEmpPhoto.Text = ofd.FileName;
           }
       }

      私有void Validation_Error(对象发送者,ValidationErrorEventArgs e)
       {
          如果(e.Action == ValidationErrorEventAction.Added)
                             _errors ++;
          其他
                             _errors--;
       }
    }
}

namespace ITA
{
    /// <summary>
    /// Interaction logic for AdminMaster.xaml
    /// </summary>
    public partial class AdminMaster : Window
    {
        private int _errors = 0;
        private Person _Person = new Person();

        public AdminMaster()
        {
            InitializeComponent();
            grid_AdminData.DataContext = _Person;
        }

        public string conn_String = "Data Source=Dell;Initial Catalog=ITA;User ID=sa;Password=12345";

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            Nullable<bool> dlgRes = ofd.ShowDialog();
            if (dlgRes == true)
            {
                txtEmpPhoto.Text = ofd.FileName;
            }
        }

        private void Validation_Error(object sender, ValidationErrorEventArgs e)
        {
            if (e.Action == ValidationErrorEventAction.Added)
                _errors++;
            else
                _errors--;
        }
    }
}

Class viewmodel

Class viewmodel

命名空间ITA
{
   公共类Person:IDataErrorInfo,INotifyPropertyChanged
    {
      公众人物()
       {

       }
      私有字符串F_Name =";
      公共字符串FullName
       {
          得到
           {
                            返回F_Name;
           }
          设置
           {
                            如果(F_Name!=值)
                             {
                    F_Name =值;
                    OnPropertyChanged("FullName");
                             }
           }
       }
      私有字符串_City =";
      公共字符串城市
       {
          得到
           {
                            返回_City;
           }
          设置
           {
                            如果(_City!=值)
                             {
                    _City =值;
                    OnPropertyChanged("City");
                             }
           }
       }
      私有字符串_State =";
      公共字符串State
       {
          得到
           {
                            返回_State;
           }
          设置
           {
                            如果(_State!=值)
                             {
                    _State =值;
                    OnPropertyChanged("State");
                             }
           }
       }
      私有字符串P_Code;
      公共字符串PostalCode
       {
          得到
           {
                            返回P_Code;
           }
          设置
           {
                            如果(P_Code!=值)
                             {
                    P_Code =值;
                    OnPropertyChanged("PostalCode");
                             }
           }
       }
      私有字符串_Address;
      公共字符串地址
       {
          得到
           {
                            返回_Address;
           }
          设置
           {
                            如果(_Address!=值)
                             {
                    _Address =值;
                    OnPropertyChanged("Address");
                             }
           }
       }
      公共事件PropertyChangedEventHandler PropertyChanged;
      受保护的void OnPropertyChanged(string propertyName)
       {
          如果(PropertyChanged!= null)
           {
                             PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
           }
       }

      公共字符串错误
       {
          得到{抛出新的NotImplementedException(); }
       }

      公共字符串this [string columnName]
       {
          得到
           {
                            字符串结果=";
                             //string charVal = @" ^ [a-zA-Z] * $";;
                            字符串txtVal = @" ^ [a-zA-Z] * \ s [a-zA-Z] * \ s [a-zA-Z] * \ s $;
                            字符串numVal = @" [0-9];
                            字符串ScharVal = @"[!|"" | _ | @ |#| \ $ |%| \?| \> | \< | \ * | \ {| \} | \\] | \ [ | \:| \; | \'| \/| \.| \,| \ + | \ = | \(| \)| \-];
                            字符串mailVal = @" ^(((([\ w] + \.[\ w] +)+)|([\ w] +))@(([[ww ++.)+)([A -Za-z] {1,3})$" ;;

                            如果(FullName.Length!= 0&&!Regex.IsMatch(FullName,txtVal))
                             {
                    result ="输入全名,例如\ n LastName-FirstName-MiddleName"
                             }
                            如果(Regex.IsMatch(FullName,numVal)|| Regex.IsMatch(FullName,ScharVal))
                             {
                    result =输入正确的全名";
                             }
                             if(Regex.IsMatch(City,numVal)|| Regex.IsMatch(City,ScharVal))
                             {
                   结果=输入正确的城市";
                             }
                             if(Regex.IsMatch(State,numVal)|| Regex.IsMatch(State,ScharVal))
                             {
                   结果=进入适当状态";
                             }
                             if(Regex.IsMatch(PostalCode,charVal)|| Regex.IsMatch(PostalCode,ScharVal))
                             {
                                      结果=输入正确的邮政编码";
                             }
                             if(Address.Length!= 0&& Address.Length< 1 || Address.Length> 50)//在这里,我得到了nullreferanceexception的错误未由用户代码处理
                             {
                   结果=输入最多50个单词";
                             }
                            返回结果;
           }
       }
    }
}

namespace ITA
{
    public class Person : IDataErrorInfo, INotifyPropertyChanged
    {
        public Person()
        {

        }
        private string F_Name = "";
        public string FullName
        {
            get
            {
                return F_Name;
            }
            set
            {
                if (F_Name != value)
                {
                    F_Name = value;
                    OnPropertyChanged("FullName");
                }
            }
        }
        private string _City = "";
        public string City
        {
            get
            {
                return _City;
            }
            set
            {
                if (_City != value)
                {
                    _City = value;
                    OnPropertyChanged("City");
                }
            }
        }
        private string _State = "";
        public string State
        {
            get
            {
                return _State;
            }
            set
            {
                if (_State != value)
                {
                    _State = value;
                    OnPropertyChanged("State");
                }
            }
        }
        private string P_Code;
        public string PostalCode
        {
            get
            {
                return P_Code;
            }
            set
            {
                if (P_Code != value)
                {
                    P_Code = value;
                    OnPropertyChanged("PostalCode");
                }
            }
        }
        private string _Address;
        public string Address
        {
            get
            {
                return _Address;
            }
            set
            {
                if (_Address != value)
                {
                    _Address = value;
                    OnPropertyChanged("Address");
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        public string this[string columnName]
        {
            get
            {
                string result = "";
                //string charVal = @"^[a-zA-Z]*$";
                string txtVal = @"^[a-zA-Z]*\s[a-zA-Z]*\s[a-zA-Z]*\s$";
                string numVal = @"[0-9]";
                string ScharVal = @"[!|""|_|@|#|\$|%|\?|\>|\<|\*|\{|\}|\]|\[|\:|\;|\'|\/|\.|\,|\+|\=|\(|\)|\-]";
                string mailVal = @"^((([\w]+\.[\w]+)+)|([\w]+))@(([\w]+\.)+)([A-Za-z]{1,3})$";

                if (FullName.Length != 0 && !Regex.IsMatch(FullName, txtVal))
                {
                    result = "Enter Full Name like\n LastName - FirstName - MiddleName ";
                }
                if (Regex.IsMatch(FullName, numVal) || Regex.IsMatch(FullName, ScharVal))
                {
                    result = "Enter Proper Full Name";
                }
                if (Regex.IsMatch(City, numVal) || Regex.IsMatch(City, ScharVal))
                {
                    result = "Enter Proper City";
                }
                if (Regex.IsMatch(State, numVal) || Regex.IsMatch(State, ScharVal))
                {
                    result = "Enter proper State";
                }
                if (Regex.IsMatch(PostalCode, charVal) || Regex.IsMatch(PostalCode, ScharVal))
                {
                    result = "Enter Proper PostalCode";
                }
                if (Address.Length != 0 && Address.Length < 1 || Address.Length > 50) // here i got this error of nullreferanceexception was unhandled by usercode
                {
                    result = "Enter maximium 50 words";
                }
                return result;
            }
        }
    }
}

推荐答案

有一个代码格式化按钮是有原因的.发布代码段时请使用它.

There is a code formatting button for a reason. Please use it when posting code snippets.

在尝试访问其任何属性之前,应检查是否已将Address属性分配给字符串:

You should check that the Address property has been assigned to a string before trying to access any its properties:

if (!string.IsNullOrEmpty(Address) && (Address.Length != 0 && Address.Length < 1 || Address.Length > 50)) 
result = "Enter maximium 50 words"; }

希望有帮助.

请记住,通过将有用的帖子标记为答案来关闭话题,然后在遇到新问题时开始新话题.请不要在同一线程中问几个问题.

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


这篇关于用户代码未处理NullReferanceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆