Please review the following code snippet. I get a nullreferenceexception in "this.directories.Add (new directory (s))". Recursion seems to work as long as it doesn't βunwindβ, at which point βnew directory (s)β seems to be null. I'm not sure why it behaves this way, I thought there might be special rules, because recursion is in the constructor. Please, help.
namespace AnalyzeDir { class directory { public string[] files; public ArrayList directories; public string mypath; public string myname; public directory(string mp) { mypath = mp; myname = mypath.Substring(mypath.LastIndexOf("\\")); files = Directory.GetFiles(mypath); fillDirectoriesRescursive(); } public void fillDirectoriesRescursive() { string[] dirpaths = Directory.GetDirectories(mypath); if (dirpaths != null && (dirpaths.Length > 0)) { foreach(string s in dirpaths) { this.directories.Add(new directory(s)); } } }
source share