C# System.NullReferenceException when trying to access DirectoryInfo on parent form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JonnyBlaze
    New Member
    • Feb 2008
    • 5

    C# System.NullReferenceException when trying to access DirectoryInfo on parent form

    Hi Guys -

    This is my first C# app, but I have some knowledge of a bunch of languages like VB6 and PHP, so I'm picking it up rather quickly. I've been pulling my hair out on this one for about 4 hours now.

    I have a DirectoryInfo on my parent form: (extra bits chopped out...)
    Code:
    public partial class frmMain : Form
    {
        public DirectoryInfo diTemp;
    
        private void frmMain_Load(object sender, EventArgs e)
        {
            diTemp = Directory.CreateDirectory("c:\\temp");
        }
    
        private void btn1B_ExportCSV_Click(object sender, EventArgs e)
        {
            frm1B_ExportCSV f = new frm1B_ExportCSV();
            f.ShowDialog(this);
        }
    
        public string setWorkingDir(string strSubdir)
        {
            diTemp.CreateSubdirectory(strSubdir);
            return diTemp.FullName + "\\" + strSubdir;
        }
    }
    And when I try to access it on my child form:
    Code:
    public partial class frm1B_ExportCSV : Form
    {
        private string strWorkingDir;
    
        private void frm1B_ExportCSV_Load(object sender, EventArgs e)
        {
            strWorkingDir = ((frmMain)this.ParentForm).setWorkingDir("1B");  
        }
    }
    it throws a System.NullRefe renceException when opening the child form at run time.

    I wish I would have kept better records of what all I've tried, but I got frustrated. I can tell you I've tried accessing the DirectoryInfo directly, and several variations of things like this
    Code:
    //Cast MDIParent to your MDIParent Form
    theMDIParentForm theParent = (theMDIParentForm) this.MDIParent;
        
    //Now you can access objects on the MDI Parent form from MDI Children
    theParent.myTreeViewControl.Nodes[0].Add(new TreeNode("New Child Node"));
    I'm sure it's something incredibly easy that I'm missing, any help would be appreciated.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I think it is happening because you derived class does not call the contructor of your base class, which is where the DirectoryInfo instance is created.
    Call the base class's constructor first, then try to use it?

    Comment

    • JonnyBlaze
      New Member
      • Feb 2008
      • 5

      #3
      Originally posted by Plater
      I think it is happening because you derived class does not call the contructor of your base class, which is where the DirectoryInfo instance is created.
      Call the base class's constructor first, then try to use it?
      Thanks for your reply... Sorry but that's a little above my head with C#. Also, would your method have the effect of creating a new DirectoryInfo object or referencing the existing one on the parent form (which is what I'm trying to do)?


      Is any of these what you mean, or close at least?

      Change
      Code:
      public partial class frm1B_ExportCSV : Form
      To
      Code:
      public partial class frm1B_ExportCSV : frmMain
      (didn't work)

      I found this also and tried it:
      Code:
      public partial class frm1B_ExportCSV : Form
      {
          private frmMain frmParent;
      
          public frm1B_ExportCSV(frmMain fFrm)
          {
              InitializeComponent();
              frmParent = fFrm;
          }
      
          private void frm1B_ExportCSV_Load(object sender, EventArgs e)
          {
              strWorkingDir = frmParent.setWorkingDir("B2");
          }
      }
      And got this error: "No overload for method 'frm1B_ExportCS V' takes '0' arguments"

      so I tried:
      Code:
      public frm1B_ExportCSV()
      {
          InitializeComponent();
          frmParent = (frmMain)this.MdiParent;
      }
      but frmParent.setWo rkingDir("B2"); still throws NullReferenceEx ception

      I feel like I'm closer but I know I'm missing an important concept or something. What else should I be trying? Am I setting the parent correctly when I call the form like this?
      Code:
      frm1B_ExportCSV f = new frm1B_ExportCSV();
      f.ShowDialog(this);

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Going back to your original code (I had mis-read it before sorry):
        Have you tried something like this:
        Code:
        private void frm1B_ExportCSV_Load(object sender, EventArgs e)
        {
           if(this.ParentForm!=null)
           {
              MessageBox.Show(this.ParentForm.GetType().Name.ToString());
           }
           //strWorkingDir = ((frmMain)this.ParentForm).setWorkingDir("1B");  
        }
        This should popup a messagebox that shows the type of the parentform if it is not null. (It should say "frmMain" in the box if you setup your child windows correctly)

        Comment

        • JonnyBlaze
          New Member
          • Feb 2008
          • 5

          #5
          Originally posted by Plater
          Going back to your original code (I had mis-read it before sorry):
          Have you tried something like this:
          Code:
          private void frm1B_ExportCSV_Load(object sender, EventArgs e)
          {
             if(this.ParentForm!=null)
             {
                MessageBox.Show(this.ParentForm.GetType().Name.ToString());
             }
             //strWorkingDir = ((frmMain)this.ParentForm).setWorkingDir("1B");  
          }
          This should popup a messagebox that shows the type of the parentform if it is not null. (It should say "frmMain" in the box if you setup your child windows correctly)
          Thanks for the help Plater. I tried this:
          Code:
                      if(this.ParentForm!=null)
                      {
                          MessageBox.Show(this.ParentForm.GetType().Name.ToString());
                      }
                      else 
                      {
                          MessageBox.Show("Null parent form");
                      }
          (tried same thing with this.MdiParent by the way, and i tried it in OnShown and frm1B_ExportCSV )

          And I get the message box, so the parent form definitely isn't getting set. But when I create the form like
          Code:
          frm1B_ExportCSV f = new frm1B_ExportCSV();
          f.ShowDialog(this);
          when I pass "this" to ShowDialog, it's supposed to create the parent/child relationship, right? Unless I'm mis-understanding the VS2005 documentation?

          I can access variables inside the child form before it's disposed like this
          Code:
          string strReturnValue = f.strReturnValue;
          string strReturnError = f.strReturnError;
          And it works just fine.

          I tried setting the parent before showing the form like this
          Code:
          frm1B_ExportCSV f = new frm1B_ExportCSV();
          f.Parent = this;
          f.ShowDialog(this);
          Which throws this error: System.Argument Exception - Top-level control cannot be added to a control.


          I can't figure out how to set the parent of the child form on the child form (this.Parent = ???) at run-time, but I'll keep trying.

          Any idea if f.ShowDialog(th is) is the proper way to set parent of the child? Or maybe I'm missing something somewhere that would set it?

          Comment

          • Shashi Sadasivan
            Recognized Expert Top Contributor
            • Aug 2007
            • 1435

            #6
            From your original post

            Code:
            public partial class frmMain : Form
            {
                public DirectoryInfo diTemp;
             
                private void frmMain_Load(object sender, EventArgs e)
                {
                    diTemp = Directory.CreateDirectory("c:\\temp");
                }
             
                private void btn1B_ExportCSV_Click(object sender, EventArgs e)
                {
                    frm1B_ExportCSV f = new frm1B_ExportCSV();
                    f.ShowDialog(this);
                }
             
                public string setWorkingDir(string strSubdir)
                {
                    diTemp.CreateSubdirectory(strSubdir);
                    return diTemp.FullName + "\\" + strSubdir;
                }
            }
            And when I try to access it on my child form:

            Code:
            public partial class frm1B_ExportCSV : Form
            {
                private string strWorkingDir;
             
                private void frm1B_ExportCSV_Load(object sender, EventArgs e)
                {
                    strWorkingDir = ((frmMain)this.ParentForm).setWorkingDir("1B");  
                }
            }
            the calling form :
            frm1B_ExportCSV f = new frm1B_ExportCSV ();
            f.ShowDialog(th is);

            the dialog Form: (you need to use the Owner property)

            Code:
            private void frm1B_ExportCSV_Load(object sender, EventArgs e)
                {
                    strWorkingDir = ((frmMain)this.Owner).setWorkingDir("1B");  
                }

            Comment

            • JonnyBlaze
              New Member
              • Feb 2008
              • 5

              #7
              Originally posted by Shashi Sadasivan
              From your original post
              Code:
              private void frm1B_ExportCSV_Load(object sender, EventArgs e)
                  {
                      strWorkingDir = ((frmMain)this.Owner).setWorkingDir("1B");  
                  }

              That was it exactly. I knew it had to be something really small like that. Thanks a million Shashi!

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                I was wondering about how you were calling it, the parent form and stuff only applies to mdi children, not owned dialogs. Glad an answer was found.

                Comment

                • JonnyBlaze
                  New Member
                  • Feb 2008
                  • 5

                  #9
                  Originally posted by Plater
                  I was wondering about how you were calling it, the parent form and stuff only applies to mdi children, not owned dialogs. Glad an answer was found.
                  Probably some misnomers on my part, I'm still getting familiar with the terminology. Thanks Plater.

                  Comment

                  Working...