Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass string parameter of a method from one class to another class in same namespace?
    primarykey
    data
    text
    <p>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 <em>initiatingTree(string filename)</em> which is in MyTreeNode class.<br> 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.<br> 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? </p> <p>This is the code in Browse button of TreeDisplay class (My main form):</p> <pre><code>if (open.ShowDialog(this) == DialogResult.OK) { txtFileName.Text = open.FileName; MytreeNodeClass treenodeClass = new MytreeNodeClass(); treenodeClass.initiatingTree(open.FileName,treeView1); } </code></pre> <p>Here is the code of initiatingTree() in my MyTreeNode class:</p> <pre><code>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 &amp; 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); } </code></pre> <p>On using breakpoint treeView1.Nodes.Clear(), the code comes out from this line and enters the catch block throwing NullReferenceException. </p> <p>Please help to find root cause of exception. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload