0

我在使用 c# 列表时遇到问题,不确定在向 Managepagesid 列表添加新对象时我错过了重点!

 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
     }

我正在尝试访问将新项目添加到 ManagePage_id 列表中,但它会引发一些空异常..帮助!

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);
            }

添加了堆栈跟踪!

Exception>System.NullReferenceExceptionObject 引用未设置为对象的实例。在 G:\Development\Vega_MongoDB\Vega_MongoDB\FBDataAccess\ClientFBRepository.cs 中的 Vega_MongoDB.FBDataAccess.ClientFBRepository.ClientsData(String accessToken) 处:G:\Development\Vega_MongoDB 中 Vega_MongoDB.Models.ClientRepository..ctor(String connection) 的第 48 行\Vega_MongoDB\Models\Clients\ClientRepository.cs:G:\Development\Vega_MongoDB\Vega_MongoDB\Models\Clients\ClientRepository.cs 中 Vega_MongoDB.Models.ClientRepository..ctor() 的第 47 行:Vega_MongoDB.Controllers.ClientsController 的第 23 行..cctor() 在 g:\Development\Vega_MongoDB\Vega_MongoDB\Controllers\ClientsController.cs:line 18

我检查了 pagedetails 对象,它包含所有数据..

谢谢

毗湿奴

4

3 回答 3

2

您应该在添加项目之前创建列表的实例:

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);
}
于 2012-10-19T14:37:08.347 回答
1

尝试将此添加到您的课程中:

public Clients()
{
  this.Manage_Pages_id = new List<ManagePages>();
}
于 2012-10-19T14:38:45.467 回答
1

您需要在添加任何内容之前初始化您的列表。您可以在Clients类的构造函数或调用代码中执行此操作(如 Artem 建议的那样)。

public class Clients
{
    //properties

    public Clients()
    {
         Manage_Pages_id = new List<ManagePages>();
    }
}
于 2012-10-19T14:39:21.547 回答