Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8630081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:58:21+00:00 2026-06-12T08:58:21+00:00

Getting a NullValueException on a Property that Isn’t null Here’s the code for the

  • 0

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]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:58:22+00:00Added an answer on June 12, 2026 at 8:58 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting this weird LINQ error. title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String Here is the code I have:
Getting this error when I follow the simple code example from jsoncpp, that basically
Getting data from a database table to an object in code has always seemed
Getting data onto inputStream object from web url inputStream = AWSFileUtil.getInputStream( AWSConnectionUtil.getS3Object(null), cdn.generalsentiment.com, filePath);
Getting red warning message that expected identifier for this statement CALayer = ImageView.layer; Using
Getting Invalid Application Path error. Here are the steps I've taken. Right click on
getting a couple errors that it's a virtual function with non-virtual destructor. how can
Getting warning message that Duplicate protocol definition of ModalViewDelegate is ignored Defined protocol in
Getting started with Django 1.3 here. Loving the system so far, but struggling on
Getting this error: Each GROUP BY expression must contain at least one column that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.