How to pass string parameter of a method from one class to another class in same namespace?

StackOverflow https://stackoverflow.com/questions/606263

  •  03-07-2019
  •  | 
  •  

Question

Little silly question, but got stuck for a long time. I have written two classes one which is a Form (TreeDisplay class) and other which contains buiseness logic (MyTreeNode class). TreeDisplay class contains browse button to select a file pass it to a method initiatingTree(string filename) which is in MyTreeNode class.
Now I have to pass this string parameter filename to MyTreeNode class. When I run my code the XML file which I have selected is shown in the textbox but not in treeview.
I have written the part of code but it is throwing NullReferenceException(Object reference not set to an instance of an object). When the whole code was writeen in Form.cs the code was running successfully but on separating the business logic the exception has occured. Can you please tell me where I am wrong?

This is the code in Browse button of TreeDisplay class (My main form):

if (open.ShowDialog(this) == DialogResult.OK)
        {
            txtFileName.Text = open.FileName;
            MytreeNodeClass treenodeClass = new MytreeNodeClass();
            treenodeClass.initiatingTree(open.FileName,treeView1); 
        }

Here is the code of initiatingTree() in my MyTreeNode class:

public class MytreeNodeClass
{

    private readonly System.Windows.Forms.TextBox txtFileName;
    private TreeView treeView1;
    private readonly ToolStripStatusLabel toolStripStatusLabel1;

   public string Filename
    {
        get { return filename; }
    }
    protected string filename;

    public MytreeNodeClass()
    {
    }

    public void initiatingTree(string nameofFile,TreeView treeView1)
    {
        try
        {
            //Create XML document & load the XML file.
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(nameofFile);
            treeView1 = new System.Windows.Forms.TreeView();
            treeView1.Nodes.Clear();

            if (xmlDocument.DocumentElement != null)
            {
                TreeNode treeNodedoc = new TreeNode(xmlDocument.DocumentElement.Name);

                treeView1.Nodes.Add(treeNodedoc);
            }

On using breakpoint treeView1.Nodes.Clear(), the code comes out from this line and enters the catch block throwing NullReferenceException.

Please help to find root cause of exception. Thanks.

Was it helpful?

Solution

What is the value of treeView1 at your breakpoint?

I suggest this may be null, as the reference isn't available in your Business Logic Layer.


Updated with sample code:

public void initiatingTree(string nameofFile, TreeView treeView1)
{
    try
    {
        //Create XML document & load the XML file.
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(nameofFile);

        treeView1.Nodes.Clear();

        if (xmlDocument.DocumentElement != null)
        {
            TreeNode treeNodedoc = new TreeNode(xmlDocument.DocumentElement.Name);

            treeView1.Nodes.Add(treeNodedoc);
        }
    }
}

and where you call this:

if (open.ShowDialog(this) == DialogResult.OK)
{
    txtFileName.Text = open.FileName;
    MytreeNodeClass treenodeClass = new MytreeNodeClass();
    treenodeClass.initiatingTree(open.FileName, treeView1); 
}

OTHER TIPS

As c.k said.

You don't have access to the usercontrols from your business layer as a standard. May i suggest that you keep you control code in the gui code behind for simplicity.

An example how to sort your code

(Gui Layer) Codebehind for control interaction
Business Layer for data manipulation
Data Layer for database manipulations and such

Initialize the treeView1 object before accessing it.

I guess you copied and pasted, right?

Anyways here's a little note: When you drag controls on a form in VS Designer, vs generates code for these controls "usually in InitializeComponent()". VS writes the references in your class (e.g form1.cs) like the following:

private System.Windows.Forms.TreeView treeView1;

this is just declaring a variable of type System.Windows.Forms.TreeView this variable (itself) is stored on the stack, because it's just a reference, a reference that doesn't point to any actual heap objects till now (i.e NullReference). Now this reference is useless and you can't use it unless you initialize it and tell it to point to a real object on the heap, and that's what VS does for you in the designer, it simply adds the next line to InitializComponents:

this.treeView1 = new System.Windows.Forms.TreeView();

And this is the line that you forgot to copy to the other class, which left your reference uninitialized (i.e still NullReference), hence when you tried to use it, a NullReferenceException was thrown. I guess this is your problem or at least this is how i could understand it. Forgive me if i failed to understand you!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top