0

I'm getting an error on some of my while loops when I try to replace some of my variables.

Below is the code for my main class:

int i = 0;

                            while (ships.Read())
                            {

                                product_id = ships.GetString(0);
                                unit_price = ships.GetDecimal(1);
                                currency = ships.GetString(2);

                                model.Events[i].Event_params.Product_id = product_id;
                                model.Events[i].Event_params.Unit_price = unit_price;
                                model.Events[i].Event_params.Currency = currency;
                                i++;


                            }

And below is my class structure to get and set the data.

 public class InsiderCRMAPI
{
    [JsonProperty("users")]
    public Collection<UserModel> User = new Collection<UserModel>();

    [JsonProperty("events")]
    public Collection<EventModel> Events = new Collection<EventModel>();
}
public class EventModel
{
    [JsonProperty("event_name")]
    public string Event_name { get; set; }

    [JsonProperty("timestamp")]
    public string Timestamp { get; set; }

    [JsonProperty("event_params")]
    public ProductModel Event_params { get; set; } = new ProductModel();

    [JsonProperty("taxonomy")]
    public Collection<TaxModel> Taxonomy = new Collection<TaxModel>();


}
public class ProductModel
{
    [JsonProperty("product_id")]
    public string Product_id { get; set; }

    [JsonProperty("unit_price")]
    public decimal Unit_price { get; set; }

    [JsonProperty("currency")]
    public string Currency { get; set; }

    [JsonProperty("custom")]
    public CustomProdModel Custom { get; set; } = new CustomProdModel();

}

Any help with how to fix the issue or find what's causing the exception would be great!

DomBlanks
  • 5
  • 1

1 Answers1

0

It's because you try to get the ith element (first, second, ...) but your not in a array where you already defined the size and every "cells" are available. You're in a collection and that collection is empty so if you try to get the first element it just crashed because there is not.

You should build your object and Add it to the collection:

        model.Events.Add(new EventModel()
        {
            Event_params = new ProductModel()
            {
                Product_id = product_id,
                Unit_price = unit_price,
                Currency = currency
            }
        });
Arcord
  • 1,724
  • 1
  • 11
  • 16
  • How do you know it is empty? OP has not shown where that is being constructed. – Crowcoder Mar 10 '21 at 16:55
  • I assumed it was the "Events" property in the InsiderCRMAPI class. He didn't show "model" was from that type but since it was the only class left... – Arcord Mar 10 '21 at 16:56
  • That's just a class definition, you can't tell anything about how `model` was modified in the program. – Crowcoder Mar 10 '21 at 16:57
  • That why I said I was assuming it. His index begin to 0 so I guessed it was the first time he was using it. I may be totally wrong but I think, from what he showed, it is the issue he has. – Arcord Mar 10 '21 at 16:59
  • You're probably right but it's not possible to tell for sure. I don't see anything that says it fails on index 0. – Crowcoder Mar 10 '21 at 17:41
  • Perfect, that solved the problem! Thanks! – DomBlanks Mar 11 '21 at 09:11