Getting a NullValueException on a Property that Isn't null
Here's the code for the beginnings of a ViewModel, and the method that creates the ViewModel object and opens the Window I am using the ViewModelt in. The exception is being thrown on the SwitchName property. The _ciscoswitch.SwitchName is coming up as null because the _ciscoswitch in the SwitchVewModel is null. The exception is getting thrown at InitializeComponent() in the SwitchBrowser constructor. Looking at the SwitchVewModel instance in the debugger, _ciscoswitch is not null. I tried changing the SwitchName accessor to use CiscoSwitch.switchName instead of _ciscoswitch.SwitchName, it still failed.
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 for the SwitchBrowserWindow the only property I am using right now is the SwitchName to try and get this working
<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>
Exception, stack trace and call stack
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:
CiscoDecodeWPF.exe!CiscoDecodeWPF.SwitchViewModel.SwitchName.get() Line 50 + 0xf bytes C# [External Code] CiscoDecodeWPF.exe!CiscoDecodeWPF.SwitchBrowser.SwitchBrowser(CiscoDecodeWPF.CiscoSwitch cs) Line 35 + 0x8 bytes C# CiscoDecodeWPF.exe!CiscoDecodeWPF.MainWindow.BrowseSwitchMenuItem_Click(object sender, System.Windows.RoutedEventArgs e) Line 1050 + 0x34 bytes C# [External Code]
Try below code in SwitchName
:
public string SwitchName
{
get {
if (_ciscoswitch != null)
{
return _ciscoswitch.switchName;
}
else
{
return string.empty;
}
}
set
{
if (_ciscoswitch != null)
{
if (_ciscoswitch.switchName != value)
{
_ciscoswitch.switchName = value;
RaisePropertyChanged("switchName");
}
}
}
}
If you don't want to check _ciscoswitch != null
in SwitchName
then put DataContext = svm
before InitizlizeComponent()
CODE:
public SwitchBrowser(CiscoSwitch cs)
{
SwitchViewModel svm = new SwitchViewModel(cs);
DataContext = svm;
InitializeComponent();
}
And remove below code from XAML
.
<Window.DataContext>
<CiscoDecodeWPF:SwitchViewModel/>
</Window.DataContext>
Hope It should Work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.