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

  • Home
  • SEARCH
  • 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 370005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:58:34+00:00 2026-05-12T13:58:34+00:00

Hey Guys, I’m trying to add to a dictionary, and recieveing the Object reference

  • 0

Hey Guys, I’m trying to add to a dictionary, and recieveing the “Object reference not set to an instance of an object.” error. Which I think means that what I’m trying to set it to doesn’t exist?

This is the relevant code:

                Dictionary<string, Dictionary<int, Dictionary<string, string>>> user = new Dictionary<string, Dictionary<int, Dictionary<string, string>>>();
            user.Add("approved", null);
            user.Add("pending", null);
            user.Add("declined", null);
            int zz = 0;

            while (results.Read())
            {
                Dictionary<string, string> field = new Dictionary<string, string>();
                for (int i = 0; i < results.FieldCount; i++)
                {
                    switch (fds[i].ToString())
                    {
                        case "gender":
                            string gend = ((Convert.ToBoolean(results[i])) == false) ? "Male" : "Female";
                            field.Add("gender", gend);
                            break;
                        default:
                            field.Add(fds[i], results[i].ToString());
                            break;
                    }
                }
                string status = results[0].ToString();
                user["approved"].Add(zz, field);
                zz++;
            }

Is there an issue with the way I am setting the three dictionaries at the beginning?
Thanks,

Psy

  • 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-12T13:58:34+00:00Added an answer on May 12, 2026 at 1:58 pm

    You have a third level in your nested dictionary structure, and you’re skipping initialization of the second level. At the very least, you need to add:

    user["approved"] = new Dictionary<int, Dictionary<string, string>>();
    

    What would probably be better, is to do initialization further up front:

    user.Add("approved", new Dictionary<int, Dictionary<string, string>>());
    user.Add("pending", new Dictionary<int, Dictionary<string, string>>());
    user.Add("declined", new Dictionary<int, Dictionary<string, string>>());
    

    Personally, I wouldn’t use a Dictionary for user at all. It implies that there are a variable number of statuses of a request (or whatever), while in fact there is a finite amount of possibilities: pending, approved, declined. In my opinion, you would be better off writing a class that holds 3 collections for that.

    This also helps in that you wouldn’t have three nested Dictionaries, making the code more readable. It was enough to confuse you, let alone someone maintaining the code after you 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 262k
  • Answers 262k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not quite what you asked, but maybe what you want… May 13, 2026 at 11:45 am
  • Editorial Team
    Editorial Team added an answer You've grouped the results - so which username would you… May 13, 2026 at 11:45 am
  • Editorial Team
    Editorial Team added an answer It passes by reference. Meaning that it won't create a… May 13, 2026 at 11:45 am

Related Questions

hey guys, i'm getting an exception on the following inner exception: {Value cannot be
Hey guys, I want to ask if there is an equivalent to the @@DBTS
Hey guys, I'm researching something for a friend of mine who's involved in road
Hey guys, I don't know RegExp yet. I know a lil about it but
Hey guys, I have a problem. I have situation where domain in m_domainTable starts

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.