943,917 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 16263
  • C# RSS
Jan 14th, 2009
0

Object reference not set to an instance of an object.

Expand Post »
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

C# Syntax (Toggle Plain Text)
  1. public string[] GetFileList()
  2. {
  3. string[] downloadFiles;
  4. StringBuilder result = new StringBuilder();
  5. FtpWebRequest reqFTP;
  6. try
  7. {
  8. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + txtftpServerName.Text + "/"));
  9. reqFTP.UseBinary = true;
  10. reqFTP.Credentials = new NetworkCredential(txtftpUserName.Text, txtftpPassword.Text);
  11. reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
  12. WebResponse response = reqFTP.GetResponse();
  13. StreamReader reader = new StreamReader(response.GetResponseStream());
  14.  
  15. string line = reader.ReadLine();
  16. while (line != null)
  17. {
  18. result.Append(line);
  19. result.Append("\n");
  20. line = reader.ReadLine();
  21. }
  22. result.Remove(result.ToString().LastIndexOf('\n'), 1);
  23. reader.Close();
  24. response.Close();
  25.  
  26. return result.ToString().Split('\n');
  27. }
  28. catch (Exception ex)
  29. {
  30. System.Windows.Forms.MessageBox.Show(ex.Message);
  31. downloadFiles = null;
  32. return downloadFiles;
  33. }
  34. }
  35.  
  36. public string[] GetChildFileList()
  37. {
  38. string[] downloadFiles;
  39. StringBuilder result = new StringBuilder();
  40. FtpWebRequest reqFTP;
  41. try
  42. {
  43. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + txtftpServerName.Text + "/" + treeView1.SelectedNode.Text + "/"));
  44. reqFTP.UseBinary = true;
  45. reqFTP.Credentials = new NetworkCredential(txtftpUserName.Text, txtftpPassword.Text);
  46. reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
  47. WebResponse response = reqFTP.GetResponse();
  48. StreamReader reader = new StreamReader(response.GetResponseStream());
  49.  
  50. string line = reader.ReadLine();
  51. while (line != null)
  52. {
  53. result.Append(line);
  54. result.Append("\n");
  55. line = reader.ReadLine();
  56. }
  57. result.Remove(result.ToString().LastIndexOf('\n'), 1);
  58. reader.Close();
  59. response.Close();
  60.  
  61. return result.ToString().Split('\n');
  62. }
  63. catch (Exception ex)
  64. {
  65. System.Windows.Forms.MessageBox.Show(ex.Message);
  66. downloadFiles = null;
  67. return downloadFiles;
  68. }
  69. }

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

C# Syntax (Toggle Plain Text)
  1.  
  2. private void btnDoIt_Click(object sender, EventArgs e)
  3. {
  4. string[] Parent_Name = GetFileList();
  5. string[] Child_Name = GetChildFileList();
  6.  
  7. treeView1.Nodes.Clear();
  8. if (Parent_Name != null)
  9. {
  10. /* Parent Node */
  11. foreach (string filename in Parent_Name)
  12. {
  13. TreeNode ParentNode = new TreeNode(filename);
  14. treeView1.Nodes.Add(ParentNode);
  15.  
  16. if (Parent_Name != null)
  17. {
  18. foreach (string filename2 in Child_Name)
  19. {
  20. TreeNode ChildNode = new TreeNode(filename2);
  21. ParentNode.Nodes.Add(ChildNode);
  22. }
  23. }
  24. }
  25. }
  26. }

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
Last edited by cVz; Jan 14th, 2009 at 10:51 am.
cVz
Reputation Points: 29
Solved Threads: 7
Junior Poster
cVz is offline Offline
139 posts
since Mar 2008
Jan 14th, 2009
0

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.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 14th, 2009
0

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
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 14th, 2009
0

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
c# Syntax (Toggle Plain Text)
  1. treeView1.Nodes.Clear();
  2. if (Parent_Name != null)
  3. {
and
c# Syntax (Toggle Plain Text)
  1. if (Parent_Name != null)
  2. {
  3. foreach (string filename2 in Child_Name)

2. I suppose you should do that:
c# Syntax (Toggle Plain Text)
  1. treeView1.Nodes.Add(ParentNode);
after you fill with data the ChildNode of this ParentNode.


Good luck
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Jan 14th, 2009
1

Re: Object reference not set to an instance of an object.

Which is what I said in a lot less of a post.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 14th, 2009
0

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
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Jan 15th, 2009
0

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
Reputation Points: 29
Solved Threads: 7
Junior Poster
cVz is offline Offline
139 posts
since Mar 2008
Jan 15th, 2009
0

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 ???
cVz
Reputation Points: 29
Solved Threads: 7
Junior Poster
cVz is offline Offline
139 posts
since Mar 2008
Jan 15th, 2009
0

Re: Object reference not set to an instance of an object.

Well if Child_name is null, then look at your function
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: newbie question
Next Thread in C# Forum Timeline: Browse Control in C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC