473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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...)
Expand|Select|Wrap|Line Numbers
  1. public partial class frmMain : Form
  2. {
  3.     public DirectoryInfo diTemp;
  4.  
  5.     private void frmMain_Load(object sender, EventArgs e)
  6.     {
  7.         diTemp = Directory.CreateDirectory("c:\\temp");
  8.     }
  9.  
  10.     private void btn1B_ExportCSV_Click(object sender, EventArgs e)
  11.     {
  12.         frm1B_ExportCSV f = new frm1B_ExportCSV();
  13.         f.ShowDialog(this);
  14.     }
  15.  
  16.     public string setWorkingDir(string strSubdir)
  17.     {
  18.         diTemp.CreateSubdirectory(strSubdir);
  19.         return diTemp.FullName + "\\" + strSubdir;
  20.     }
  21. }
  22.  
And when I try to access it on my child form:
Expand|Select|Wrap|Line Numbers
  1. public partial class frm1B_ExportCSV : Form
  2. {
  3.     private string strWorkingDir;
  4.  
  5.     private void frm1B_ExportCSV_Load(object sender, EventArgs e)
  6.     {
  7.         strWorkingDir = ((frmMain)this.ParentForm).setWorkingDir("1B");  
  8.     }
  9. }
  10.  
it throws a System.NullReferenceException 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
Expand|Select|Wrap|Line Numbers
  1. //Cast MDIParent to your MDIParent Form
  2. theMDIParentForm theParent = (theMDIParentForm) this.MDIParent;
  3.  
  4. //Now you can access objects on the MDI Parent form from MDI Children
  5. theParent.myTreeViewControl.Nodes[0].Add(new TreeNode("New Child Node"));
  6.  
I'm sure it's something incredibly easy that I'm missing, any help would be appreciated.
Feb 24 '08 #1
8 5216
Plater
7,872 Expert 4TB
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?
Feb 25 '08 #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?
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
Expand|Select|Wrap|Line Numbers
  1. public partial class frm1B_ExportCSV : Form
To
Expand|Select|Wrap|Line Numbers
  1. public partial class frm1B_ExportCSV : frmMain
(didn't work)

I found this also and tried it:
Expand|Select|Wrap|Line Numbers
  1. public partial class frm1B_ExportCSV : Form
  2. {
  3.     private frmMain frmParent;
  4.  
  5.     public frm1B_ExportCSV(frmMain fFrm)
  6.     {
  7.         InitializeComponent();
  8.         frmParent = fFrm;
  9.     }
  10.  
  11.     private void frm1B_ExportCSV_Load(object sender, EventArgs e)
  12.     {
  13.         strWorkingDir = frmParent.setWorkingDir("B2");
  14.     }
  15. }
  16.  
And got this error: "No overload for method 'frm1B_ExportCSV' takes '0' arguments"

so I tried:
Expand|Select|Wrap|Line Numbers
  1. public frm1B_ExportCSV()
  2. {
  3.     InitializeComponent();
  4.     frmParent = (frmMain)this.MdiParent;
  5. }
  6.  
but frmParent.setWorkingDir("B2"); still throws NullReferenceException

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?
Expand|Select|Wrap|Line Numbers
  1. frm1B_ExportCSV f = new frm1B_ExportCSV();
  2. f.ShowDialog(this);
  3.  
Feb 25 '08 #3
Plater
7,872 Expert 4TB
Going back to your original code (I had mis-read it before sorry):
Have you tried something like this:
Expand|Select|Wrap|Line Numbers
  1. private void frm1B_ExportCSV_Load(object sender, EventArgs e)
  2. {
  3.    if(this.ParentForm!=null)
  4.    {
  5.       MessageBox.Show(this.ParentForm.GetType().Name.ToString());
  6.    }
  7.    //strWorkingDir = ((frmMain)this.ParentForm).setWorkingDir("1B");  
  8. }
  9.  
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)
Feb 26 '08 #4
Going back to your original code (I had mis-read it before sorry):
Have you tried something like this:
Expand|Select|Wrap|Line Numbers
  1. private void frm1B_ExportCSV_Load(object sender, EventArgs e)
  2. {
  3.    if(this.ParentForm!=null)
  4.    {
  5.       MessageBox.Show(this.ParentForm.GetType().Name.ToString());
  6.    }
  7.    //strWorkingDir = ((frmMain)this.ParentForm).setWorkingDir("1B");  
  8. }
  9.  
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:
Expand|Select|Wrap|Line Numbers
  1.             if(this.ParentForm!=null)
  2.             {
  3.                 MessageBox.Show(this.ParentForm.GetType().Name.ToString());
  4.             }
  5.             else 
  6.             {
  7.                 MessageBox.Show("Null parent form");
  8.             }
  9.  
  10.  
(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
Expand|Select|Wrap|Line Numbers
  1. frm1B_ExportCSV f = new frm1B_ExportCSV();
  2. f.ShowDialog(this);
  3.  
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
Expand|Select|Wrap|Line Numbers
  1. string strReturnValue = f.strReturnValue;
  2. string strReturnError = f.strReturnError;
  3.  
And it works just fine.

I tried setting the parent before showing the form like this
Expand|Select|Wrap|Line Numbers
  1. frm1B_ExportCSV f = new frm1B_ExportCSV();
  2. f.Parent = this;
  3. f.ShowDialog(this);
  4.  
Which throws this error: System.ArgumentException - 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(this) is the proper way to set parent of the child? Or maybe I'm missing something somewhere that would set it?
Feb 26 '08 #5
Shashi Sadasivan
1,435 Expert 1GB
From your original post

Expand|Select|Wrap|Line Numbers
  1. public partial class frmMain : Form
  2. {
  3.     public DirectoryInfo diTemp;
  4.  
  5.     private void frmMain_Load(object sender, EventArgs e)
  6.     {
  7.         diTemp = Directory.CreateDirectory("c:\\temp");
  8.     }
  9.  
  10.     private void btn1B_ExportCSV_Click(object sender, EventArgs e)
  11.     {
  12.         frm1B_ExportCSV f = new frm1B_ExportCSV();
  13.         f.ShowDialog(this);
  14.     }
  15.  
  16.     public string setWorkingDir(string strSubdir)
  17.     {
  18.         diTemp.CreateSubdirectory(strSubdir);
  19.         return diTemp.FullName + "\\" + strSubdir;
  20.     }
  21. }
And when I try to access it on my child form:

Expand|Select|Wrap|Line Numbers
  1. public partial class frm1B_ExportCSV : Form
  2. {
  3.     private string strWorkingDir;
  4.  
  5.     private void frm1B_ExportCSV_Load(object sender, EventArgs e)
  6.     {
  7.         strWorkingDir = ((frmMain)this.ParentForm).setWorkingDir("1B");  
  8.     }
  9. }
the calling form :
frm1B_ExportCSV f = new frm1B_ExportCSV();
f.ShowDialog(this);

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

Expand|Select|Wrap|Line Numbers
  1. private void frm1B_ExportCSV_Load(object sender, EventArgs e)
  2.     {
  3.         strWorkingDir = ((frmMain)this.Owner).setWorkingDir("1B");  
  4.     }
Feb 26 '08 #6
From your original post
Expand|Select|Wrap|Line Numbers
  1. private void frm1B_ExportCSV_Load(object sender, EventArgs e)
  2.     {
  3.         strWorkingDir = ((frmMain)this.Owner).setWorkingDir("1B");  
  4.     }

That was it exactly. I knew it had to be something really small like that. Thanks a million Shashi!
Feb 27 '08 #7
Plater
7,872 Expert 4TB
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.
Feb 27 '08 #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.
Probably some misnomers on my part, I'm still getting familiar with the terminology. Thanks Plater.
Feb 29 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
1
by: Rafael | last post by:
Hi, I hope I can find some help for this problem IDE: Visual Studio.NET 2003 Developer Editio Language: C# Problem: "An unhandled exception of type 'System.NullReferenceException' occurred in...
2
by: Maurice Mertens | last post by:
Hi, I'm having some troubles with a .NET application. I have a MDI-parent form and MDI-child forms. When I press a shortcut key in a MDI-child form (i.e. <ALT> + <D>) I get the following...
11
by: Andreas Wirén | last post by:
Hi I'm a C# .NET newbie doing a minor school project but I'm getting a strange error message. 'System.NullReferenceException' occurred in system.windows.forms.dll Additional information: Object...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
7
by: Peter Afonin | last post by:
Hello, I'm using this code to access a network share from an asp.net page: Dim dir As DirectoryInfo = New DirectoryInfo("\\10.0.0.150\FormLib\") Dim files() As FileInfo = dir.GetFiles("*.eps")...
0
by: Glenn Venzke | last post by:
I have an aspx page that is set up to copy backed-up DB files from a shared directory to a local folder. For some reason, it is being denied access to the network share. I have the web app running...
1
by: Jeff | last post by:
hey asp.net 2.0 The line "e.Item.Parent.ChildItems.Remove(e.Item);" gives this error: 'e.Item' threw an exception of type 'System.NullReferenceException' System.Web.UI.WebControls.MenuItem...
1
by: nygiantswin2005 | last post by:
Hi I am trying to resolve this bug that I have in this application. The code is below. It will generate this Exception System.UnauthorizedAccessException: Access to the path is denied. at...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.