NullReference question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Moe haroon
    New Member
    • Oct 2010
    • 4

    NullReference question

    Hi All, I'm very new to c#, programming in general :-)

    I'm trying to do a simple test and its failing, hopfully you guru's can answer it in a jiffy...

    Setup:
    - Form1 with toolstrip and toolstriplabel with value "TEST"
    - Form2 with a Button

    Test: Button press will change the label value on Form1 toolstriplabel to "PASSED";

    Here is the code... [not working]

    Code:
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                
            }
    
            private void Form1_Shown(object sender, EventArgs e)
            {
                Form2 ChildForm = new Form2();
                ChildForm.ShowDialog();
            }
        }
    }
    Here is Form2

    Code:
    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    Form1 MainForm = new Form1();
                    MainForm.Controls["toolStripStatusLabel1"].Text = "Passed";
                    
                }
                catch (NullReferenceException nre)
                {
                    MessageBox.Show("Error:\n" + nre.Message);
                }
            }
        }
    }
    I'm getting NullReferenceEx ception... What do I need to instantiate?
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    You're creating a new instance of Form1, not getting the one that already exists. If you plan on doing anything with Form1 in Form2, you'll need a reference to it.

    You could modify Form2's:
    Code:
    private Form1 form1;
    
    public Form2(Form1 form1)
    {
      InitializeComponent();
      this.form1 = form1;
    }
    And you'd have to create it like this:
    Code:
    Form2 childForm = new Form2(this);
    childForm.ShowDialog();
    But it's bad practice to directly manipulate a parent's controls from a child form.

    What you should do is make a public property on Form1 that gets the value you're looking for, and use that.

    Comment

    • Moe haroon
      New Member
      • Oct 2010
      • 4

      #3
      thanks Curtis for a quick reply... I'm still getting the same error... not sure If I got all the instructions you posted placed correctly.... Here is the code

      Form1:
      Code:
      namespace WindowsFormsApplication1
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
                  
              }
      
              private void Form1_Shown(object sender, EventArgs e)
              {
                  Form2 ChildForm = new Form2(this);
                  ChildForm.ShowDialog();
              }
          }
      }
      Form2:
      Code:
      namespace WindowsFormsApplication1
      {
          public partial class Form2 : Form
          {
              private Form1 form1;
      
              public Form2(Form1 form1)
              {
                  InitializeComponent();
                  this.form1 = form1;
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  try
                  {
                     form1.Controls["toolStripStatusLabel1"].Text = "Passed";
                      
                  }
                  catch (NullReferenceException nre)
                  {
                      MessageBox.Show("Error:\n" + nre.Message);
                  }
              }
          }
      }
      After the change I am still getting the same error

      Comment

      • Aimee Bailey
        Recognized Expert New Member
        • Apr 2010
        • 197

        #4
        When using dialogues, you should use the Owner mechanism...

        Code:
        namespace WindowsFormsApplication1
        {
            public partial class Form1 : Form
            {
                Form2 otherForm;
        
                public Form1()
                {
                    InitializeComponent();
                }
        
                private void Form1_Shown(object sender,
                                         EventArgs e)
                {
                    otherForm = new Form2();
                    otherForm.ShowDialog(this);
                }
        
            }
        }
        Code:
        namespace WindowsFormsApplication1
        {
            public partial class Form2 : Form
            {
                public Form2()
                {
                    InitializeComponent();
                }
        
                private void button1_Click(object sender,
                                           EventArgs e)
                {
                    Form1 form1 = (Form1)this.Owner;
                    form1.toolStripStatusLabel1.Text = "Passed";
                }
            }
        }
        this ensures that the parent is passed correctly.

        Aimee.

        Comment

        • Moe haroon
          New Member
          • Oct 2010
          • 4

          #5
          Thanks Aimee, that did the trick. As stated in my post I'm a beginer and would like to follow best practices. Would you concur with Curtis earlier reply to this post that I should be using the public property rather than going the route of having child forms manipulate objects in parent form.

          On that note I've made some modification, still trying to figure out how the whole property value set option works, please see the code below and advise how to go about utilizing the set value

          thanks

          Code:
          namespace WindowsFormsApplication1
          {
              public partial class Form1 : Form
              {
          
                  Form2 ChildForm;
          
                  public Form1()
                  {
                      InitializeComponent();
                  }
          
                  public class GetTSLabel
                  {
                      private string LabelValue = string.Empty;
                      public string Value
                      {
                          get { return LabelValue; }
                          set { LabelValue = value; }
                      }
                     
                  }
          
                  /* RESOLVED BASED ON AIMEE's POST
                  private void Form1_Shown(object sender, EventArgs e)
                  {
                      ChildForm = new Form2();
                      ChildForm.ShowDialog(this);
                  }
                   */
          
                  private void Form1_Shown(object sender, EventArgs e)
                  {
                      ChildForm = new Form2();
                      ChildForm.ShowDialog();
                  }
                  
                  
              }
          }
          Form2:
          Code:
          namespace WindowsFormsApplication1
          {
              public partial class Form2 : Form
              {
          
                  public Form2()
                  {
                      InitializeComponent();
          
                  }
          
                  private void button1_Click(object sender, EventArgs e)
                  {
                      try
                      {
                          Form1.GetTSLabel TSLabel = new Form1.GetTSLabel();
                          TSLabel.Value = "Passed";
                          MessageBox.Show("Name: " + TSLabel.Value);
          
                          /* RESOLVED BASED ON AIMEE's POST
                            Form1 form1 = (Form1)this.Owner;
                          form1.toolStripStatusLabel1.Text = "Passed";
                           */ 
                      }
                      catch (NullReferenceException nre)
                      {
                          MessageBox.Show("Error:\n" + nre.Message);
                      }
                  }
              }
          }
          When i run the following I can see the TSLabel.Value is set to "PASSED". I guess what I'm asking is how do I go about setting the toolstriplabel. text to TSLabel.Value{s et in Form2} in Form1

          thanks Moe

          Comment

          • Aimee Bailey
            Recognized Expert New Member
            • Apr 2010
            • 197

            #6
            I think it's ok to modify parent controls as they each implement their own properties anyway. Properties at the end of the day are there to help access/transport, so unless you require a level of direction control, I don't see the point in restricting access to the controls on the parent.

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              I think it's ok to modify parent controls as they each implement their own properties anyway.
              I'm sorry, but that's just bad practice. Forms should be as self-contained as possible. You shouldn't be directly modifying a form's UI elements from another form. If you were to delete or rename a TextBox, for example, then now you have to trace down everywhere else you used that TextBox.

              If you use a Property, though, all you have to do is change the get and set methods, and it's updated everywhere.

              That's the whole point of properties. That's also the reason why fields shouldn't be public. It's called "encapsulat ion" and it's a well known and accepted Object Oriented design pattern.

              Comment

              • Aimee Bailey
                Recognized Expert New Member
                • Apr 2010
                • 197

                #8
                I am a loyal follower of the OOP practices, allthough im self minded enough to know that sometimes it is capable of having it's own impracticalitie s, You are right, properties make more sense on this topic. But the user posting the question requested a solution to a non strict OOP problem, rather than a brief history on encapsulation.

                When it comes to WinForms or indeed WPF, I believe that actually it is bad practice to use a model that allows for non-persistant interfaces, no matter how pretty, disrupting the interface can confuse the user. This is where HCI comes into the picture and we start to ask "why would you delete or rename a TextBox in the first place?".

                Alot of people brush past HCI now days without any clue as to why it's there in the first place, heck im sure there are a vast many people with VS open right now with absolutely no understanding of the subject, I deeply encourage everyone to take a look at what it is, because OOP is not the be all and end all.

                Aimee.

                Comment

                Working...