獲取有關非空空值異常的專屬非空
下面是一個ViewModel的開端,創建視圖模型對象,並打開窗戶,我的代碼,該方法的屬性一NullValueException我正在使用ViewModelt。在SwitchName屬性上拋出異常。 _ciscoswitch.SwitchName由於在SwitchVewModel中的_ciscoswitch爲空而變爲空值。異常是在SwitchBrowser構造函數中的InitializeComponent()處拋出。在調試器中查看SwitchVewModel實例,_ciscoswitch不爲空。我嘗試將SwitchName訪問器更改爲使用CiscoSwitch.switchName而不是_ciscoswitch.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
}
}
XAML的SwitchBrowserWindow我使用的是現在唯一的財產就是試圖得到這方面的工作
<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
的InnerException的交換機名稱:
思科DecodeWPF.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# [外部代碼]
這幾乎肯定是你的XAML出了問題。請張貼(並刪除一些C#) – ChrisF
另外,發佈整個堆棧跟蹤。 –
通過'New SwitchBrowser(cs.FirstOrDefault())'中的'FirstOrDefault',您可以很容易地將'null'作爲'CiscoSwitch'傳入......並且如果您的代碼使用默認的'公共SwitchViewModel()'構造函數'_ciscoswitch '也將'null' ... – nemesv