Object reference not set to an instance of an object.

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 135
Reputation: cVz is an unknown quantity at this point 
Solved Threads: 7
cVz's Avatar
cVz cVz is offline Offline
Junior Poster

Object reference not set to an instance of an object.

 
0
  #1
Jan 14th, 2009
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

  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:

  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.
Delphi & C# programmer deluxe...
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

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

 
0
  #2
Jan 14th, 2009
It works with me! but try to debug, that's the one who you can fully trust in.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

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

 
0
  #3
Jan 14th, 2009
Its almost certainly correct:

as you go to use Child_name, but you checked parent wasnt null, so child could be
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

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

 
0
  #4
Jan 14th, 2009
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
  1. treeView1.Nodes.Clear();
  2. if (Parent_Name != null)
  3. {
and
  1. if (Parent_Name != null)
  2. {
  3. foreach (string filename2 in Child_Name)

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


Good luck
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

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

 
1
  #5
Jan 14th, 2009
Which is what I said in a lot less of a post.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

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

 
0
  #6
Jan 14th, 2009
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
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 135
Reputation: cVz is an unknown quantity at this point 
Solved Threads: 7
cVz's Avatar
cVz cVz is offline Offline
Junior Poster

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

 
0
  #7
Jan 15th, 2009
Welll well well, i feel like a retard now hahahahahahahaha!!!

Although , thats not the problem...

The problem lies here

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

"The remote server returned an error: (550) File unavailable. (eg. File not found, unavailable)."
Obviously there is still some kind of error
Delphi & C# programmer deluxe...
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 135
Reputation: cVz is an unknown quantity at this point 
Solved Threads: 7
cVz's Avatar
cVz cVz is offline Offline
Junior Poster

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

 
0
  #8
Jan 15th, 2009
Look i know the problemo is this

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 ???
Delphi & C# programmer deluxe...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

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

 
0
  #9
Jan 15th, 2009
Well if Child_name is null, then look at your function
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC