DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   Object reference not set to an instance of an object. (http://www.daniweb.com/forums/thread168344.html)

cVz Jan 14th, 2009 10:50 am
Object reference not set to an instance of an object.
 
Ah man , ive been adjusting the code so much ...

I am trying to make a treeview that display's all the directories and files on theserver (SO FAR SO GOOD) ...

Here's my code to retrieve Directories and files

public string[] GetFileList()
        {
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + txtftpServerName.Text + "/"));
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(txtftpUserName.Text, txtftpPassword.Text);
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                WebResponse response = reqFTP.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
               
                string line = reader.ReadLine();
                while (line != null)
                {
                    result.Append(line);
                    result.Append("\n");
                    line = reader.ReadLine();
                }
                result.Remove(result.ToString().LastIndexOf('\n'), 1);
                reader.Close();
                response.Close();
               
                return result.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                downloadFiles = null;
                return downloadFiles;
            }
        }

public string[] GetChildFileList()
        {
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + txtftpServerName.Text + "/" + treeView1.SelectedNode.Text + "/"));
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(txtftpUserName.Text, txtftpPassword.Text);
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                WebResponse response = reqFTP.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());

                string line = reader.ReadLine();
                while (line != null)
                {
                    result.Append(line);
                    result.Append("\n");
                    line = reader.ReadLine();
                }
                result.Remove(result.ToString().LastIndexOf('\n'), 1);
                reader.Close();
                response.Close();

                return result.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                downloadFiles = null;
                return downloadFiles;
            }
        }

This works fine indevidually , but when i try to combine the two:


private void btnDoIt_Click(object sender, EventArgs e)
        {
            string[] Parent_Name = GetFileList();
            string[] Child_Name = GetChildFileList();

            treeView1.Nodes.Clear();
            if (Parent_Name != null)
            {
                /*          Parent Node            */
                foreach (string filename in Parent_Name)
                {
                    TreeNode ParentNode = new TreeNode(filename);
                    treeView1.Nodes.Add(ParentNode);

                    if (Parent_Name != null)
                    {
                        foreach (string filename2 in Child_Name)
                        {
                            TreeNode ChildNode = new TreeNode(filename2);
                            ParentNode.Nodes.Add(ChildNode);
                        }
                    }
                }
            }
        }

i get an error stating that "Object reference not set to an instance of an object." (LENS ON [foreach (string filename2 in Child_Name)] )

Can anyone maybe give me a clue ?? i dont even know why its complaining because it aught to work...

Happy coding

Ramy Mahrous Jan 14th, 2009 11:33 am
Re: Object reference not set to an instance of an object.
 
It works with me! but try to debug, that's the one who you can fully trust in.

LizR Jan 14th, 2009 11:48 am
Re: Object reference not set to an instance of an object.
 
Its almost certainly correct:

as you go to use Child_name, but you checked parent wasnt null, so child could be

Antenka Jan 14th, 2009 3:49 pm
Re: Object reference not set to an instance of an object.
 
It also works proper with me, but have some thing, that you should look at:
1. You check Parent_Name 2 times. And second one is inside the first one. It looks like question: "Are you really sure that (Parent_Name != null)" ;P
treeView1.Nodes.Clear();
            if (Parent_Name != null)
            {
and
if (Parent_Name != null)
                    {
                        foreach (string filename2 in Child_Name)

2. I suppose you should do that:
treeView1.Nodes.Add(ParentNode);
after you fill with data the ChildNode of this ParentNode.


Good luck :)

LizR Jan 14th, 2009 3:57 pm
Re: Object reference not set to an instance of an object.
 
Which is what I said in a lot less of a post.

Antenka Jan 14th, 2009 4:09 pm
Re: Object reference not set to an instance of an object.
 
Oh yeah :( sorry. First part is your's. Wasn't about to do things like that O.O ... it seems that I need rest a bit ;P

cVz Jan 15th, 2009 1:47 am
Re: Object reference not set to an instance of an object.
 
Welll well well, i feel like a retard now hahahahahahahaha!!!

Although , thats not the problem...

The problem lies here

Quote:

string[] Child_Name = GetChildFileList();
for some reason (i think) the Uri does not change because you have to SELECT a node first...

So i did this

private void btnDoIt_Click(object sender, EventArgs e)
        {
            string[] Parent_Name = GetFileList();
           

            treeView1.Nodes.Clear();
            if (Parent_Name != null)
            {

                /*          Parent Node            */
                foreach (string filename in Parent_Name)
                {
                    TreeNode ParentNode = new TreeNode(filename);
                   
                    // changed the GetChildFileList uri to 'reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + txtftpServerName.Text + "/" + txtShowDirectory.Text + "/"));'

                    txtShowDirectory.Text = filename;

                    string[] Child_Name = GetChildFileList();

                    if (Child_Name != null)
                    {
                        foreach (string filename2 in Child_Name)
                        {
                            TreeNode ChildNode = new TreeNode(filename2);
                            ParentNode.Nodes.Add(ChildNode);
                        }
                    }
                    treeView1.Nodes.Add(ParentNode);
                }
            }
        }

WORKS LIKE A BOMB!!!!!!!!

cept i still get an error on my try -> catch saying

Quote:

"The remote server returned an error: (550) File unavailable. (eg. File not found, unavailable)."
Obviously there is still some kind of error

cVz Jan 15th, 2009 2:03 am
Re: Object reference not set to an instance of an object.
 
Look i know the problemo is this

Quote:

string[] Child_Name = GetChildFileList();

if (Child_Name != null)
{
foreach (string filename2 in Child_Name)
{
TreeNode ChildNode = new TreeNode(filename2);
ParentNode.Nodes.Add(ChildNode);
}
}
but i cant really bypass that one or can I ???

LizR Jan 15th, 2009 2:57 am
Re: Object reference not set to an instance of an object.
 
Well if Child_name is null, then look at your function


All times are GMT -4. The time now is 8:30 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC