null ではないプロパティで NullValueException を取得する
ViewModel の開始部分のコードと、ViewModel オブジェクトを作成し、ViewModelt を使用しているウィンドウを開くメソッドを次に示します。SwitchName プロパティで例外がスローされています。SwitchVewModel の _ciscoswitch が null であるため、_ciscoswitch.SwitchName が null として表示されます。SwitchBrowser コンストラクターの InitializeComponent() で例外がスローされます。デバッガーで SwitchVewModel インスタンスを見ると、_ciscoswitch は null ではありません。_ciscoswitch.SwitchName の代わりに CiscoSwitch.switchName を使用するように SwitchName アクセサを変更しようとしましたが、それでも失敗しました。
class SwitchViewModel : INotifyPropertyChanged
{
#region Construction
/// Constructs the default instance of a SwitchViewModel
public SwitchViewModel()
{
}
public SwitchViewModel(CiscoSwitch cs)
{
_ciscoswitch = cs;
}
#endregion
#region Members
CiscoSwitch _ciscoswitch;
#endregion
#region Properties
public CiscoSwitch CiscoSwitch
{
get
{
return _ciscoswitch;
}
set
{
_ciscoswitch = value;
}
}
public string SwitchName
{
get { return _ciscoswitch.switchName; }
set
{
if (_ciscoswitch.switchName != value)
{
_ciscoswitch.switchName = value;
RaisePropertyChanged("switchName");
}
}
}
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Methods
private void RaisePropertyChanged(string propertyName)
{
// take a copy to prevent thread issues
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
SwitchBrowserWindow の XAML は、私が現在使用している唯一のプロパティは、これを機能させるための SwitchName です。
<Window x:Class="CiscoDecodeWPF.SwitchBrowser"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CiscoDecodeWPF="clr-namespace:CiscoDecodeWPF" IsEnabled="{Binding Enabled}"
Title="SwitchBrowser" Height="500" Width="500" Background="GhostWhite">
<Window.Resources>
<Style TargetType="{x:Type TreeViewItem}" x:Key="ModuleStyle">
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="FontSize" Value="12"/>
</Style>
<Style TargetType="{x:Type TreeViewItem}" x:Key="RedModuleStyle" BasedOn="{StaticResource ModuleStyle}">
<Setter Property="Foreground" Value="Red"/>
</Style>
</Window.Resources>
<Window.DataContext>
<CiscoDecodeWPF:SwitchViewModel/>
</Window.DataContext>
<Grid Margin="0,0,-211.4,-168">
<StackPanel HorizontalAlignment="Stretch" Name="StackPanel1" VerticalAlignment="Stretch" Width="Auto" Margin="0,0,188.6,114">
<StackPanel.Resources>
<Style TargetType="{x:Type Label}" x:Key="LabelStyle">
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</StackPanel.Resources>
<Label Content="Switch Name:" Name="Label1" Height="25" HorizontalAlignment="Left"/>
<Label Content="Software Version:" Name="Label2" HorizontalAlignment="Left" />
<Label Content="Model Number:" Name="Label3" HorizontalAlignment="left"/>
<Label Content="IP Address:" Name="Label4" HorizontalAlignment="left"></Label>
<Label Content="Serial Number:" Name="Label5" HorizontalAlignment="Left"></Label>
<Label Content="Show Tech Taken:" Name="Label6" HorizontalAlignment="left"/>
</StackPanel>
<StackPanel Margin="105,0,218,489">
<StackPanel.Resources>
<Style TargetType="{x:Type Label}" x:Key="LabelStyle">
<Setter Property="FontSize" Value="12"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</StackPanel.Resources>
<Label Content="{Binding Path=SwitchName}" Name="SwitchNameLabel" HorizontalAlignment="left"/>
<Label Content="Version" Name="VersionLabel" HorizontalAlignment="left"/>
<Label Content="Model" Name="ModelNumberLabel" HorizontalAlignment="Left"/>
<Label Content="IP" Name="IPAddressLabel" HorizontalAlignment="Left"/>
<Label Content="Serial" Name="SerialLabel" HorizontalAlignment="Left"/>
<Label Content="ST" Name="ShowTechLabel" HorizontalAlignment="Left"/>
</StackPanel>
例外、スタック トレース、コール スタック
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=CiscoDecodeWPF
StackTrace:
at CiscoDecodeWPF.SwitchViewModel.get_SwitchName() in
d:\Projects\CiscoDecodeWPF\CiscoDecodeWPF\SwitchViewModel.cs:line 50
内部例外:
CiscoDecodeWPF.exe!CiscoDecodeWPF.SwitchViewModel.SwitchName.get() 50 行目 + 0xf バイト C# [外部コード] CiscoDecodeWPF.exe!CiscoDecodeWPF.SwitchBrowser.SwitchBrowser(CiscoDecodeWPF.CiscoSwitch cs) 35 行目 + 0x8 バイト C# CiscoDecodeWPF.exe!CiscoDecodeWPF. MainWindow.BrowseSwitchMenuItem_Click(オブジェクト送信者, System.Windows.RoutedEventArgs e) 1050 行 + 0x34 バイト C# [外部コード]