0

I have a problem with c# List, not sure where I'm missing the point while adding a new object to the Managepagesid List!

 public class Clients
    {
        [BsonId]
        public string Id { get; set; } //Object ID managed by MongoDb

        public string Name { get; set; } //Client Name

        public string Phone { get; set; } //Client Phone

        public string Email { get; set; } //Client E-mail

        public string Username { get; set; } //Client Username

        public DateTime LastModified { get; set; } //Client Last Login

        public string FB_User_Id { get; set; } //Client FB User ID

        public AccessToken UserAccessToken { get; set; } //AccessToken which is stored while user is logged in.

        public List<ManagePages> Manage_Pages_id { get; set; } //The pages maintained by the specific client
     }

I'm trying to access add a new item into ManagePage_id list but its thrwing some null exception.. Help!

Clients client = new Clients();

            client.FB_User_Id = FBData.id;
            client.Name = FBData.name;
            client.Email = FBData.email;
            client.Username = FBData.username;

            for (int index = 0; index < FBData.accounts["data"].Count; index++)
            {
                ManagePages pagedetails = new ManagePages()
                {
                    page_id = FBData.accounts["data"][index].id,
                    page_name = FBData.accounts["data"][index].name,
                    ManagePages_AccessTokens = new AccessToken()
                    {
                        AccessToken_accessToken = FBData.accounts["data"][index].access_token,
                        AccessToken_expiryDate = DateTime.Now
                    },
                    ManagePages_category = FBData.accounts["data"][index].category
                };

                var category = pagedetails.ManagePages_category;

                client.Manage_Pages_id.Add(pagedetails);
            }

Stack Trace added!

Exception>System.NullReferenceExceptionObject reference not set to an instance of an object. at Vega_MongoDB.FBDataAccess.ClientFBRepository.ClientsData(String accessToken) in g:\Development\Vega_MongoDB\Vega_MongoDB\FBDataAccess\ClientFBRepository.cs:line 48 at Vega_MongoDB.Models.ClientRepository..ctor(String connection) in g:\Development\Vega_MongoDB\Vega_MongoDB\Models\Clients\ClientRepository.cs:line 47 at Vega_MongoDB.Models.ClientRepository..ctor() in g:\Development\Vega_MongoDB\Vega_MongoDB\Models\Clients\ClientRepository.cs:line 23 at Vega_MongoDB.Controllers.ClientsController..cctor() in g:\Development\Vega_MongoDB\Vega_MongoDB\Controllers\ClientsController.cs:line 18

And I have checked the pagedetails object, it contains all the data..

Thanks

Vishnu

2
  • can you also add the stacktrace ? Commented Oct 19, 2012 at 14:36
  • Where are you seeing the exception? Commented Oct 19, 2012 at 14:37

3 Answers 3

2

You should create an instance of the list before adding item:

client.Manage_Pages_id = new List<ManagePages>();
for (int index = 0; index < FBData.accounts["data"].Count; index++)
{
    ManagePages pagedetails = new ManagePages()
    {
        page_id = FBData.accounts["data"][index].id,
        page_name = FBData.accounts["data"][index].name,
        ManagePages_AccessTokens = new AccessToken()
        {
            AccessToken_accessToken = FBData.accounts["data"][index].access_token,
            AccessToken_expiryDate = DateTime.Now
        },
        ManagePages_category = FBData.accounts["data"][index].category
    };

    var category = pagedetails.ManagePages_category;

    client.Manage_Pages_id.Add(pagedetails);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome Bro! It works like a charm!! I'll mark as answer in 8 minutes!
Good, answer, but I woulds recommend putting it the constructor in you class for DRY (Do not repeat yourself).
1

Try adding this to your class:

public Clients()
{
  this.Manage_Pages_id = new List<ManagePages>();
}

1 Comment

Yes, Made a note of it! Thanks :)
1

You need to initialize your list before adding anything to it. You can do this in the constructor for the Clients class or in your calling code (as Artem suggested).

public class Clients
{
    //properties

    public Clients()
    {
         Manage_Pages_id = new List<ManagePages>();
    }
}

1 Comment

Yes, AJ.. Worked like a charm!

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.