Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 156351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:18:53+00:00 2026-05-11T10:18:53+00:00

Little silly question, but got stuck for a long time. I have written two

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:18:53+00:00Added an answer on May 11, 2026 at 10:18 am

    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);  } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe a silly question but here goes anyway. Example: Let's say I have one
I have a little silly question. I have installed a PostgreSQL DB Server, but
Sorry if this question sounds a little silly, but I am not sure what
I have a silly, little class FileSystemSize which can be used both as an
A little stuck here. I have a simple question I guess. Given the following
This may be a silly question but... Say you have a sentence like: The
Please excuse (or improve the title) but I have a silly little problem that's
I am trying to learn a little c++ and I have a silly question.
I have a silly little game that I made just for myself, but I
Hey there, I got a little silly question.. After a healthy dose of some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.