| | |
Object reference not set to an instance of an object.
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
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
This works fine indevidually , but when i try to combine the two:
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
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)
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:
C# Syntax (Toggle Plain Text)
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
Last edited by cVz; Jan 14th, 2009 at 10:51 am.
Delphi & C# programmer deluxe...
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
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
and
2. I suppose you should do that:
after you fill with data the ChildNode of this ParentNode.
Good luck
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)
treeView1.Nodes.Clear(); if (Parent_Name != null) {
c# Syntax (Toggle Plain Text)
if (Parent_Name != null) { foreach (string filename2 in Child_Name)
2. I suppose you should do that:
c# Syntax (Toggle Plain Text)
treeView1.Nodes.Add(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
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
Welll well well, i feel like a retard now hahahahahahahaha!!!
Although , thats not the problem...
The problem lies here
for some reason (i think) the Uri does not change because you have to SELECT a node first...
So i did this
WORKS LIKE A BOMB!!!!!!!!
cept i still get an error on my try -> catch saying
Obviously there is still some kind of error
Although , thats not the problem...
The problem lies here
•
•
•
•
string[] Child_Name = GetChildFileList();
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)."
Delphi & C# programmer deluxe...
Look i know the problemo is this
but i cant really bypass that one or can I ???
•
•
•
•
string[] Child_Name = GetChildFileList();
if (Child_Name != null)
{
foreach (string filename2 in Child_Name)
{
TreeNode ChildNode = new TreeNode(filename2);
ParentNode.Nodes.Add(ChildNode);
}
}
Delphi & C# programmer deluxe...
![]() |
Similar Threads
- value of string is cannot conerted into system.array (VB.NET)
- object reference not set to instance of object+axis2 (Java)
- Object reference not set to an instance of an object. (VB.NET)
- Object reference not set to an instance of an object (ASP.NET)
- How to set attribute to the controls in datagrid? (ASP.NET)
- sql data pull with no result gives Object reference not set exception (VB.NET)
Other Threads in the C# Forum
- Previous Thread: newbie question
- Next Thread: Browse Control in C#
| Thread Tools | Search this Thread |
.net access algorithm angle array asp.net barchart bitmap box broadcast c# capturing check checkbox client combobox control conversion csharp custom database datagrid datagridview dataset datetime dbconnection degrees delegate design development disappear draganddrop drawing encryption enum eventhandlers excel file firefox form format forms function gdi+ image index input install java label leak libraries list listbox loop mandelbrot math monodevelop mouseclick msword mysql operator path pause photoshop picturebox pixelinversion post programming radians regex remoting resourcefile richtextbox round server sleep socket sql statistics stream string table tcpclientchannel text textbox thread time timer update usercontrol validation virtualization visualbasic visualstudio webbrowser windows winforms wpf xml







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