0
public sealed class KnowledgeBase
{
    public KnowledgeBase(string json)
    {
        KnowledgeBaseItemId = "109";
        Description = "description";
        Filename = "abc.txt";
        TagInfo.Add("tagid","tagname");

    }

    public string KnowledgeBaseItemId { get; set; }

    public string Description { get; set; }

    public string Filename { get; set; }

    public string FCKText { get; set; }

    public string DateCreated { get; set; }

    public IDictionary<string,string> TagInfo  { get; set; }

}

This is my code snippet. It sets and get values for KnowledgeBaseItemId, Description, Filename, and so on.

But it throws an exception, when I try to add/set values to IDictionary in this line

TagInfo.Add("tagid","tagname");.

Exception is:

Object reference not set to an instance of an object.

Please help me what I'm doing wrong. Thanks in Advance!

Kishor Bikram Oli
  • 1,748
  • 1
  • 22
  • 40

2 Answers2

2

Your TagInfo dictionary is not initialized.

add TagInfo=new Dictionary<string,strin>() before adding element.

Eser
  • 12,346
  • 1
  • 22
  • 32
1

assign default value using Auto-property initializer for C#6 or higher.

public IDictionary<string,string> TagInfo  { get; set; } = new Dictionary<string,string>();
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118