tags:

views:

678

answers:

4

Hi Friends,
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.

+4  A: 

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); 
}
ck
so where do i have to make the changes to remove this exception ?
crazy_itgal
cahnge your method to accept the treeview as a parameter, then you can pass it in from your UI layer
ck
If you dont mind can u provide one line syntax how to do that..Thanks
crazy_itgal
crazy_itgal
calling treeView1 = new TreeView() is destroying the reference. Also, you do not need a private TreeView in your class, as you should be passing in the treeview that is on your form. You do have a treeview on your form called treeView1?
ck
Yes i do have a treeview declaration like this for my TreeDisplay class in Form[Designer].cs private System.Windows.Forms.TreeView treeView1;Di i have to do any changes here? Else how to declare these in my MyTreeNode class?
crazy_itgal
A: 

Initialize the treeView1 object before accessing it.

i did that too but nothing changed.
crazy_itgal
A: 

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!

Galilyou
Hi,thanks for clearing the doubt.u are right i pasted it on MytreeNode class.But i tried the above syntax too still got the same xception can you plz tell where exactly do i have to put this line of code?
crazy_itgal
actually it depends, i mean, will you use this object inside this function only or do you want to use it, on a class level. anyways Put this line of code anywhere before using the object, anywhere before this line treeView1.Nodes.Clear();. recommended: at the start of the function or the constructor
Galilyou
crazy_itgal
+1  A: 

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

Sammy Elal
Sorry to say i didnt get what you mean by this....i am new to C# so have very little idea about this...i dont know what to put exactly??Thanks..
crazy_itgal