I have the following class
public class ResProductSetupData
{
public List<ProductSetup> data { get; set; }
}
public class ProductSetup
{
public List<Fundtype> FundType { get; set; }
}
public class Fundtype
{
public string FundType { get; set; }
public bool IsGIO { get; set; }
public string ReportCode { get; set; }
public List<Fundlist> FundList { get; set; }
}
public class Fundlist
{
public string FundCode { get; set; }
public string FundDesc { get; set; }
public decimal MinAllocation { get; set; }
public string VPMSField { get; set; }
public string FundSpec { get; set; }
}
Now I want to initialize this object inside another file so that I can fill its values. Fundtype is a list and FundList is another list inside FundType.
ProductSetup prodSetup = new ProductSetup();
Fundtype fundType = new Fundtype();
Fundlist fundlist = new Fundlist();
So Inside a foreach loop I instantiated these objects
prodSetup.FundType = new List<Fundtype>();
foreach (var fund in fundTypeData)
{
fundType.FundType = fund.FundType;
fundType.IsGIO = fund.IsGIO;
fundType.ReportCode = "";
prodSetup.FundType.Add(fundType);
var listOfFund = GetProductFund(prodSetup.ProdCode, fund.FundType);
int i = 0;
foreach (var listFundData in listOfFund)
{
var fundDetails = GetFundDetails(listFundData.FundCode);
fundlist.FundCode = listFundData.FundCode;
fundlist.VPMSField = listFundData.VPMSField;
fundlist.FundDesc = fundDetails[0].FundDesc;
fundlist.MinAllocation = fundDetails[0].MinAllocation;
fundlist.FundSpec = fundDetails[0].FundSpec;
prodSetup.FundType[i].FundList.Add(fundlist);
i++;
}
}
When I add data into the Fundlist list, an error will show System.NullReferenceException: 'Object reference not set to an instance of an object.'. I know I have to instantiate first, but I dont know how. Any help would be appreciated.
Update, I have instantiated the FundList array as below
int i = 0;
foreach (var listFundData in listOfFund)
{
prodSetup.FundType[i].FundList = new List<Fundlist>();
var fundDetails = GetFundDetails(listFundData.FundCode);
fundlist.FundCode = listFundData.FundCode;
fundlist.VPMSField = listFundData.VPMSField;
fundlist.FundDesc = fundDetails[0].FundDesc;
fundlist.MinAllocation = fundDetails[0].MinAllocation;
fundlist.FundSpec = fundDetails[0].FundSpec;
prodSetup.FundType[i].FundList.Add(fundlist);
i++;
}
The first index [0] is fine. But when it loops to the second index to instantiate the second index [1], it throws another error Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index
Related
I'm trying to realize the listBox with List where i getting some variable.
And when i wonna to add a new "Object" to static List i getting a NullReferenceException
This is my code where i adding a new List
if (EventArgsIn.Message.Chat.Id == MainChatId)
{
if (EventArgsIn.Message.ReplyToMessage != null)
{
var _tempMessage = new listBoxMessage()
{
From = EventArgsIn.Message.From.Username,
FromId = EventArgsIn.Message.From.Id,
MessageId = EventArgsIn.Message.MessageId,
MessageText = EventArgsIn.Message.Text,
MessageIdReply = 0
};
tempMessageMain.Add(_tempMessage);
} else
{
var _tempMessage = new listBoxMessage() {
From = EventArgsIn.Message.From.Username,
FromId = EventArgsIn.Message.From.Id,
MessageId = EventArgsIn.Message.MessageId,
MessageText = EventArgsIn.Message.Text,
MessageIdReply = 0
};
tempMessageMain.Add(_tempMessage);
}
}
And here is my static List
public static List<listBoxMessage> tempMessageMain;
A-a-and my class where i doing Template
public class listBoxMessage
{
public listBoxMessage()
{
}
public string From { get; set; }
public int FromId { get; set; }
public int MessageId { get; set; }
public string MessageText { get; set; }
public int MessageIdReply { get; set; }
}
}
This is test code*
You declared a listbox with next line:
public static List<listBoxMessage> tempMessageMain;
The value of tempMessageMain is still null.
Now you have to create a new instance of tempMessageMain:
public static List<listBoxMessage> tempMessageMain = new List<listBoxMessage>();
I've sourced my data for a treeview using the Proces datastructure below. expanded simply indicates whether or not the tree item has been expanded to show all of its children. This is my attempt to iterate through a map all showing items into a DataTable.
public class Proces
{
public string PN { get; set; }
public string Description { get; set; }
public int Qty { get; set; }
public string PartType { get; set; }
public decimal PricePer { get; set; }
public string Mfr { get; set; }
public int Stock { get; set; }
public int OnOrder { get; set; }
public string parent { get; set; }
public bool expanded { get; set; } = false;
public List<Proces> subProcesses { get; set; }
}
I'm trying to map this out into a DataTable, but i keep getting a stack overflow.
void generateShownTree(List<Proces> proccess)
{
foreach (Proces proc in processes)
{
DataRow drNew = export.NewRow();
drNew["Parent"] = proc.parent;
drNew["PN"] = proc.PN;
drNew["Description"] = proc.Description;
drNew["Qty"] = proc.Qty;
drNew["PartType"] = proc.PartType;
drNew["PricePer"] = proc.PricePer;
drNew["Mfr"] = proc.Mfr;
drNew["Stock"] = proc.Stock;
drNew["OnOrder"] = proc.OnOrder;
export.Rows.Add(drNew);
if (proc.expanded == true)
{
foreach (Proces subProc in proc.subProcesses)
{
subProc.parent = proc.PN;
drNew = export.NewRow();
drNew["Parent"] = subProc.parent;
drNew["PN"] = subProc.PN;
drNew["Description"] = subProc.Description;
drNew["Qty"] = subProc.Qty;
drNew["PartType"] = subProc.PartType;
drNew["PricePer"] = subProc.PricePer;
drNew["Mfr"] = subProc.Mfr;
drNew["Stock"] = subProc.Stock;
drNew["OnOrder"] = subProc.OnOrder;
export.Rows.Add(drNew);
generateShownTree(proc.subProcesses);
}
}
}
}
I assume you don't want to iterate the list of subprocesses as well as invoke the generateShownTree method recursively. I also changed the name of the argument passed to generateShownTree to match the object being iterated.
static void generateShownTree(List<Proces> processes)
{
foreach (Proces proc in processes)
{
DataRow drNew = export.NewRow();
drNew["Parent"] = proc.parent;
drNew["PN"] = proc.PN;
drNew["Description"] = proc.Description;
drNew["Qty"] = proc.Qty;
drNew["PartType"] = proc.PartType;
drNew["PricePer"] = proc.PricePer;
drNew["Mfr"] = proc.Mfr;
drNew["Stock"] = proc.Stock;
drNew["OnOrder"] = proc.OnOrder;
export.Rows.Add(drNew);
if (proc.expanded)
{
generateShownTree(proc.subProcesses);
}
}
}
I'm trying to create an object and insert to the database but keep getting the same error no matter what I try.
The row that I get the error on is ColumnGroupTest.ValidValues.Add(memberComment1); the error is
error message
NullReferenceException was unhandled by user code
my models
public class StoreColumnName
{
public int Id { get; set; }
public string StoreColumnGroupName { get; set; }
public string ColumnName { get; set; }
public string ColumnType { get; set; }
public List<StoreValidValue> ValidValues { get; set; }
}
public class StoreValidValue
{
public int Id { get; set; }
public string ValidValue { get; set; }
public StoreColumnName StoreColumnName { get; set; }
}
my controller
public ActionResult Index()
{
XDocument document = XDocument.Load(#"C:\Users\Physical.xml");
var result = document.Descendants("ColumnGroup");
foreach(var item in result){
var ColumnGroupName = item.Attribute("name").Value;
var Columns = item.Descendants("Column");
foreach (var itemColumn in Columns)
{
StoreColumnName ColumnGroup = new StoreColumnName();
var ColumnGroupTest = new StoreColumnName
{
StoreColumnGroupName = ColumnGroupName,
ColumnName = itemColumn.Attribute("name").Value,
ColumnType = itemColumn.Attribute("type").Value,
Id = 11
};
var ValidValues = itemColumn.Descendants("ValidValues");
var Values = ValidValues.Descendants("Value");
foreach (var Value in Values)
{
var memberComment1 = new StoreValidValue
{
StoreColumnName = ColumnGroupTest,
ValidValue = Value.Value,
Id = 101
};
ColumnGroupTest.ValidValues.Add(memberComment1);
}
}
}
return View();
}
(I gladly take tips on what I can improve when asking for help/guiding here).
Can anyone help ?
The issue that you're having is that you don't initialize your ValidValues property to a list. By default, those types of properties initialize to null unless you specify differently.
The best approach is to add that initialization to your constructor of that object.
public StoreColumnName() {
this.ValidValues = new List<StoreValidValue>();
}
I have a list defined as below in each of 11 different classes (which handle web-services)
private List<edbService> genEdbService;
internal class edbService
{
public string ServiceID { get; set; }
public string ServiceName { get; set; }
public string ServiceDescr { get; set; }
public string ServiceInterval { get; set; }
public string ServiceStatus { get; set; }
public string ServiceUrl { get; set; }
public string SourceApplication { get; set; }
public string DestinationApplication { get; set; }
public string Function { get; set; }
public string Version { get; set; }
public string userid { get; set; }
public string credentials { get; set; }
public string orgid { get; set; }
public string orgunit { get; set; }
public string customerid { get; set; }
public string channel { get; set; }
public string ip { get; set; }
}
The list is populated in each class by reading the web-service configuration data from xml files in each class:
public DCSSCustomerCreate_V3_0()
{
try
{
XElement x = XElement.Load(global::EvryCardManagement.Properties.Settings.Default.DataPath + "CustomerCreate.xml");
// Get global settings
IEnumerable<XElement> services = from el in x.Descendants("Service")
select el;
if (services != null)
{
edb_service = new List<edbService>();
// edb_service= Common.populateEDBService("CustomerCreate.xml");
foreach (XElement srv in services)
{
edbService edbSrv = new edbService();
edbSrv.ServiceID = srv.Element("ServiceID").Value;
edbSrv.ServiceName = srv.Element("ServiceName").Value;
edbSrv.ServiceDescr = srv.Element("ServiceDescr").Value;
edbSrv.ServiceInterval = srv.Element("ServiceInterval").Value;
edbSrv.ServiceStatus = srv.Element("ServiceStatus").Value;
edbSrv.ServiceUrl = srv.Element("ServiceUrl").Value;
foreach (XElement ServiceHeader in srv.Elements("ServiceHeader"))
{
...
now what I want to do is have this code in one place in my Common.cs class so I tried:
public static List<edbService> populateEDBService(string xmlDataFile)
{
try
{
XElement x = XElement.Load(global::EvryCardManagement.Properties.Settings.Default.DataPath + xmlDataFile);
// Get global settings
IEnumerable<XElement> services = from el in x.Descendants("Service")
select el;
if (services != null)
{
//edb_Service = new List<edbService>();
foreach (XElement srv in services)
{
edbService edbSrv = new edbService();
edbSrv.ServiceID = srv.Element("ServiceID").Value;
edbSrv.ServiceName = srv.Element("ServiceName").Value;
edbSrv.ServiceDescr = srv.Element("ServiceDescr").Value;
edbSrv.ServiceInterval = srv.Element("ServiceInterval").Value;
edbSrv.ServiceStatus = srv.Element("ServiceStatus").Value;
edbSrv.ServiceUrl = srv.Element("ServiceUrl").Value;
foreach (XElement ServiceHeader in srv.Elements("ServiceHeader"))
{
edbSrv.SourceApplication = ServiceHeader.Element("SourceApplication").Value;
edbSrv.DestinationApplication = ServiceHeader.Element("DestinationApplication").Value;
edbSrv.Function = ServiceHeader.Element("Function").Value;
edbSrv.Version = ServiceHeader.Element("Version").Value;
foreach (XElement ClientContext in ServiceHeader.Elements("ClientContext"))
{
edbSrv.userid = ClientContext.Element("userid").Value;
edbSrv.credentials = ClientContext.Element("credentials").Value;
edbSrv.orgid = ClientContext.Element("orgid").Value;
edbSrv.orgunit = ClientContext.Element("orgunit").Value;
edbSrv.customerid = ClientContext.Element("customerid").Value;
edbSrv.channel = ClientContext.Element("channel").Value;
edbSrv.ip = ClientContext.Element("ip").Value;
}
}
// populateEDBService.Add(edbSrv);
}
}
}
catch (Exception ex)
{
/* Write to log */
Common.logBuilder("CustomerCreate : Form --> CustomerCreate <--", "Exception", Common.ActiveMQ,
ex.Message, "Exception");
/* Send email to support */
emailer.exceptionEmail(ex);
}
return;
}
Now I get a compile error on the return; saying that An object of a type convertible to 'System.Collections.Generic.List<EvryCardManagement.Common.edbService>' is required
and in the class that should call this method, I want to do something like:
edb_service = Common.populateEDBService("CustomerUpdate.xml");
but I get an error Cannot implicitly convert type 'System.Collections.Generic.List<EvryCardManagement.Common.edbService>' to 'System.Collections.Generic.List<EvryCardManagement.CustomerUpdate.edbService>'
So firstly how should I return the list from my generic method and how should I call it to return the list populated with the configuration data?
It sounds like you have your class edbService defined in two namespaces,
EvryCardManagement.Common and
EvryCardManagement.CustomerUpdate
I would suggest defining it in only EvryCardManagement.Common and have everything reference it from there.
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>();
}
}