获取有关非空空值异常的专属非空
下面是一个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