0

I have this code in the main wpf window:

private void ButtonChangePermissions_Click(object sender, RoutedEventArgs e)
{
    if (ComboBoxSelectedProfile.SelectedIndex != -1)
    {
        ChangePermissionsWindow cpWindow = new ChangePermissionsWindow { parent = this };
        cpWindow.Show();
    }
    else
    {
        MessageBox.Show("Please choose a profile first.");
    }
}

This is the child wpf window code:

public partial class ChangePermissionsWindow : Window
{
        private readonly string dbConnectionString = Properties.Settings.Default.dbConnectionString;
        public postLoginWindow parent { get; set; }

        public ChangePermissionsWindow()
        {
            InitializeComponent();
            ComboBoxValuesToShow();
        }

        private void ComboBoxValuesToShow()
        {
            using (SqlConnection connection = new SqlConnection(dbConnectionString))
            {
                try
                {
                    connection.Open();

                    if (TableFunctions.doesTableExist("ProfilePermissions", dbConnectionString))
                    {
                        string selectQuery = "SELECT Permissions from ProfilePermissions where ProfileName = @ProfileName";

                        using (SqlCommand command = new SqlCommand(selectQuery, connection))
                        {
                            command.Parameters.AddWithValue("@ProfileName", parent.ComboBoxSelectedProfile.Text);//This line produces the Null reference error
                            ...Does not matter from here
                        }

For some reason the line:

command.Parameters.AddWithValue("@ProfileName", parent.ComboBoxSelectedProfile.Text)

causes a NullReferenceException.

This is the exception documentation:

System.NullReferenceException: Object reference not set to an instance of an object.
at WpfApplication1.Windows.ChangePermissionsWindow.ComboBoxValuesToShow() in c:\Users\Censored\Documents\Visual Studio 2013\Projects\WpfApplication1\WpfApplication1\Windows\WindowChangePermissions.xaml.cs:line 38}
System.Exception {System.NullReferenceException

I will appreciate your help very much!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
WeinForce
  • 1,264
  • 2
  • 11
  • 17

1 Answers1

1

At a very high-level using the Object initialization syntax follows these steps...

  1. Create an instance of the desired class by calling the appropriate constructor
  2. Call all of the listed property setters
  3. Return the newly initialized object

ComboBoxValuesToShow is called from the constructor but based on the listed steps the parent property (which is used in the method) is not set until after the constructor returns, because the property setters are not called until after the instance is created. Therefore the parent property will always be null in this case.

There are a couple of ways that come to mind to solve this...

  • If you want to use the parent property in the constructor then pass the value into the constructor and initialize the property internally.

Or

  • Make the ComboBoxValuesToShow method accessible from the parent window and move the call to ComboBoxValuesToShow from the constructor to the event handler in the parent window.
Patrick B.
  • 69
  • 1
  • Your explanation is GOLD, i'm amazed how clearly you made it for me. Is there any way to subscribe to you? – WeinForce Jan 17 '17 at 16:28