473,803 Members | 4,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

5 New Member
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.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
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 5260
Plater
7,872 Recognized Expert Expert
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
JonnyBlaze
5 New Member
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_ExportCS V' 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.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?
Expand|Select|Wrap|Line Numbers
  1. frm1B_ExportCSV f = new frm1B_ExportCSV();
  2. f.ShowDialog(this);
  3.  
Feb 25 '08 #3
Plater
7,872 Recognized Expert Expert
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
JonnyBlaze
5 New Member
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.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?
Feb 26 '08 #5
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
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(th is);

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
JonnyBlaze
5 New Member
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 Recognized Expert Expert
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
JonnyBlaze
5 New Member
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
9146
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 system.windows.forms.dll
1
536
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 system.windows.forms.dll. Additional information: Object reference not set to an instance of an object. Visual Studio's IDE is not allowing me to develop projects in C#... I've trying to create a simple Windows Forms project using Visual Studio.NET...
2
1509
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 error: An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll
11
3764
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 reference not set to an instance of an object." As my references look I seem to be using the old framework(1.0.3705) for this, but re-referencing to the newer framework(1.1.4322) dll's produces the same error. This happens under all...
2
7850
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 in treeview i display the content item in axWebBrowser, i close the sub form normally when i close the main the following error is generated An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll ...
7
4808
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") When I try to do it, I get this error: System.UnauthorizedAccessException: Access to the path
0
459
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 under a domain account that I know for a fact has access. it works fine when I log on to the network and browse the directory manually. Even when I grant full control to everyone on the share and the underlying folder itself, I still can't access...
1
1754
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 {System.NullReferenceException} The is the code where the exception occur: protected void mnuSidebar_MenuItemDataBound(object sender, MenuEventArgs e)
1
4606
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 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.__Error.WinIOError() at System.IO.FileInfo.MoveTo(String destFileName)
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10548
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.