Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6959159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:14:09+00:00 2026-05-27T15:14:09+00:00

Please look at the following code snippet. I’m getting a nullreferenceexception at this.directories.Add(new directory(s));.

  • 0

Please look at the following code snippet. I’m getting a nullreferenceexception at “this.directories.Add(new directory(s));”. The recursion seems to be working until it “unwinds”, at which point “new directory(s)” seems to be a null. I’m not sure why it is behaving this way, I thought perhaps there are special rules because the 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));
                }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:14:10+00:00Added an answer on May 27, 2026 at 3:14 pm

    You haven’t initialized directories to be anything.

    I’d suggest not doing this in the constructor, but instead moving it to a method call. It goes back to “implied behavior”, where you shouldn’t have big complicated foreign system logic inside of property accessors.

    You are also using a very old construct, ArrayList. I’d suggest using an ICollection<directory> instead. More type-safety and all that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

am new here. i have a slight problem; PLease look at the following code
Getting the above error in following code. How to rectify it. Thanks. Please look
please look at the following code first. #! /usr/bin/perl package foo; sub new {
Please look at the following code: client.Credentials = new NetworkCredential(SMTP_SERVER_USERNAME, SMTP_SERVER_PASSWORD); client.EnableSsl = false;
Please have look on the following code: $_SESSION[process_y] = new Process(); $process_y = $_SESSION[process_y];
Please look at the following code: #include <iostream> using namespace std; class A {
Please take a look at following code: #include <stdio.h> #include <iostream> using namespace std;
Please look this link : (Jquery Time Picker Add-on) timepicker example ( Time picker
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
Please take a look at the following code: Public Sub Method(Of TEntity As EntityObject)(ByVal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.