0

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

  • 1
    How about `public List FundList { get; set; } = new List();` and possibly even removing the setter altogether? – Lasse V. Karlsen Dec 01 '20 at 08:28
  • @LasseV.Karlsen the problem here is that his `prodSetup.FundType` is empty (unless relevant piece of code is missing) so accessing `[i]` will throw out of range – Rafalon Dec 01 '20 at 08:30
  • Perhaps `FundType` shouldn't be a list then? An autopopulating dictionary or something, or you need to prepopulate it. `List` does not have any magic that can help you. – Lasse V. Karlsen Dec 01 '20 at 08:32
  • Can you show the foreach? I think a Linq select query can be a solution. – vernou Dec 01 '20 at 08:34
  • Added the foreach – HellRyder 43 Dec 01 '20 at 08:34
  • 1
    *an error will show* - maybe copy/paste the error in your question too? – Rafalon Dec 01 '20 at 08:36
  • Added the error message – HellRyder 43 Dec 01 '20 at 08:40
  • I suspect a null reference exception: adding `fundType.FundList = new List();` below `fundType.ReportCode = "";` should solve it (or Lasse's original comment). However I think your classes could be slightly redesigned – Rafalon Dec 01 '20 at 08:40
  • Sorry if I tell you something very trivial. But it is what solves your problem: https://www.google.com/search?q=nullreferenceexception Specifically, this question: https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it. If you don't read the answer thoroughly, you're not going to solve your current and future problems. – Mohammad Dehghan Dec 01 '20 at 08:48
  • 1
    Also note that, objects in C# are [*references*](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). You should instantiate `fundType` and `fundList` in their respective loops, or you'll end up with the same object in all the items of `prodSetup.FundType` and `prodSetup.FundType[i].FundList`. – Mohammad Dehghan Dec 01 '20 at 08:52
  • 1
    @MohammadDehghan you are missing the point. That is why I am asking how to instantiate it, I have no problem instantiating another object. I am asking HOW, because it is a list inside another list. – HellRyder 43 Dec 01 '20 at 08:59
  • I have updated my question – HellRyder 43 Dec 01 '20 at 09:02
  • I don't get exactly get what your problem is. May be this: `prodSetup = new ProductSetup(); prodSetup.FundType = new List();` ? – Mohammad Dehghan Dec 01 '20 at 09:47
  • You get `NullReferenceException`, but you didn't tell us where it happens (exact place in code). You could also easily find out which variable or property is null, by using your debugger (Visual Studio). You also didn't post a complete code that we could test. We don't know what is the return value of your methods and what are the values in your code. – Mohammad Dehghan Dec 01 '20 at 09:48
  • You can't access `prodSetup.FundType[1]` because you added only one item to this list. When you iterate through a list, and start using `[i]` you need to think: "*will there be an element when `i` reaches 1, 2, 3,... ?*" - when do you actually add an item to the list? you should add as many items as `i` goes up, therefore the filling should be done in the loop itself… or shouldn't, because maybe the part where you're wrong is when you do `[i]`, this is what I meant when I said maybe your classes could be redefined – Rafalon Dec 01 '20 at 10:35
  • To go further, your `Fundlist` class doesn't seem to actually be a list, and therefore should be named `Fund`. This way, your `FundList` *property* will become a `List` which is much more reflecting the fact that it is a list of funds, and not a list of fundlists (`List`) - the property could also just be named `Funds` (note: plural) – Rafalon Dec 01 '20 at 10:42
  • The same way, `prodSetup.FundType` should be `prodSetup.FundTypes` and then you start asking yourself "*how many fund types do I have? do I have `i` fund types?*" – Rafalon Dec 01 '20 at 10:54

0 Answers0