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 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

Related Questions

Hey guys I'm trying to add the string 'url( ) ' around my a
Hey guys, I have created a tree which is not a binary tree. Now,
Hey guys i am trying to automate my system at work for sending artwork
Hey guys, given a data set in plain text such as the following: ==Events==
Hey guys, I'm trying to get my head around LINQ and FP, so forgive
Hey guys I can't seem to get the syntax here right, I'm just trying
Hey guys I'm getting three things right now that I'm trying to take care
Hey guys I am struggling to create a query which gives schedule of a
Hey guys, I always get confused by this so it's not exactly my strong
Hey guys, I have a GridView which is binded to SqlDataSource, everything works fine,

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.