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
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);
}
Try adding this to your class:
public Clients()
{
this.Manage_Pages_id = new List<ManagePages>();
}
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>();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.