Click here to Skip to main content
16,020,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a static dictionary

public static Dictionary<Guid, Dictionary<string, object>> Store {get; set;}

I want to add value to the above dictionary from another class as

Guid clientId = Guid.NewGuid();
Dictionary<string,object> storageKeyDictionary = new Dictionary<string, object>() { { "parameter", "test" }};

Storage.Store.Add(clientId, storageKeyDictionary);

But it return error as "Object reference not set to an instance of an object." while executing, please tell me what is wrong with the code?

What I have tried:

I have tried to add value to private static dictionary but returned error.
Posted
Updated 18-Jun-17 21:14pm

Your code is almost correct, but before Storage.Store.Add() you are missing:
C#
Store = new Dictionary<Guid, Dictionary<string, object>>();
 
Share this answer
 
you have to initialize your store object first.Use like below

public class Program
   {
       public static Dictionary<Guid, Dictionary<string, object>> Store {get; set;}

       static Program()
       {
           Store=new  Dictionary<Guid, Dictionary<string, object>>();
       }
       public static void Main(string[] args)
       {

           Guid clientId = Guid.NewGuid();
           Dictionary<string,object> storageKeyDictionary = new Dictionary<string, object>() { { "parameter", "test" }};

           Store.Add(clientId, storageKeyDictionary);

           //Your code goes here
           Console.WriteLine("Hello, world!");
       }
   }

or you can directly write like
public class Program
    {
        public static Dictionary<Guid, Dictionary<string, object>> Store =new  Dictionary<Guid, Dictionary<string, object>>();
        
      
        public static void Main(string[] args)
        {
            
            Guid clientId = Guid.NewGuid();
            Dictionary<string,object> storageKeyDictionary = new Dictionary<string, object>() { { "parameter", "test" }};

            Store.Add(clientId, storageKeyDictionary);
            
            //Your code goes here
            Console.WriteLine("Hello, world!");
        }
    }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900