0

I have a model class like this

  namespace ConnectBLL.DTO.Response
{
    public class CategorySettings
    {
        public bool NeedsLoginToViewLongText { get; set; }
        public bool NeedsLoginToViewAnyDetails { get; set; }
        public bool ShowAttachment { get; set; }
        public string CategoryPageID { get; set; }
        public string TpUrl { get; set; }
    }

    public class CategorySettingsListResponse
    {
        public List<CategorySettings> CategorySettingsList { get; set; }
    }
}

And I am trying to add data to it like this

    private readonly CategorySettings cs = new CategorySettings();
 CategorySettingsListResponse csr=new CategorySettingsListResponse();
 public string GetAllCategorySettings()
    {


            cs.NeedsLoginToViewLongText = true;
            cs.NeedsLoginToViewAnyDetails = false;
            cs.ShowAttachment = true;
            cs.CategoryPageID = "1";
            cs.TpUrl = "url";
            csr.CategorySettingsList.Add(cs);

    }

But this fails and gives an error

Object reference not set to an instance of an object.

Can any one point out what is I am doing wrong?

2
  • 1
    Where is cs defined ? Commented Mar 4, 2014 at 13:16
  • @Ofiris I missed it in the question. added now Commented Mar 4, 2014 at 13:20

4 Answers 4

2

Somewhere, you need to initialize CategorySettingsList.

public class CategorySettingsListResponse
{
    CategorySettingsListResponse() {
        CategorySettingsList = new List<CategorySettings>();
    }

    public List<CategorySettings> CategorySettingsList { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You are tying to use an instance of List before initializing. Before

csr.CategorySettingsList.Add(cs);

Insert:

if (csr.CategorySettingsList == null) {
    csr.CategorySettingsList = new List<CategorySettings>();
}

1 Comment

or just put it in the CategorySettingsListResponse constructor
1

You are using uncreated objects cs and CategorySettingsList, you should create them before use:

public string GetAllCategorySettings()
{
    csr.CategorySettingsList = new ListCategorySettings<>();
    var cs = new CategorySettings
        {
           NeedsLoginToViewLongText = true,
           ...

Comments

1

What is cs? Something missing?

You forgot to do this:

 var cs = new CategorySettings();

Also

You need to instantiate the CategorySettingsList in constructor for CategorySettingsListResponse.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.