Reputation: 8681
I am getting an error in my c# program. The error that I am getting is Index was out of range. Must be non-negative and less than the size of the collection.
At this line of code
captiveViewSeriesData.data[0].Low = CapitalViewGraphData[0];
While debugging I can see that captiveViewSeriesData.data
is null
but I am re-Initializing it.
Do I need to convert the Data as an array? Could somebody tell me what the problem is?
public class SeriesGeneric<T> where T : class
{
public List<T> Data { get; set; }
}
[TsType]
public class Data
{
public decimal Low { get; set; }
public decimal Q1 { get; set; }
public decimal Median { get; set; }
public decimal Q3 { get; set; }
public decimal High { get; set; }
}
[TsType]
public class BoxPlotSeries
{
public string color { get; set; }
public string name { get; set; }
public List<Data> data { get; set; }
}
public SeriesGeneric<BoxPlotSeries> ChartSeries
{
get
{
BoxPlotSeries captiveViewSeriesData = null;
if (CapitalViewGraphData != null && CapitalViewGraphData.Length >= 5)
{
captiveViewSeriesData = new BoxPlotSeries();
captiveViewSeriesData.color = "#FFB81C";
captiveViewSeriesData.name = "Captive";
captiveViewSeriesData.data = new List<Data>();
captiveViewSeriesData.data[0].Low = CapitalViewGraphData[0];
captiveViewSeriesData.data[0].Q1 = CapitalViewGraphData[1];
captiveViewSeriesData.data[0].Median = CapitalViewGraphData[2];
captiveViewSeriesData.data[0].Q3 = CapitalViewGraphData[3];
captiveViewSeriesData.data[0].High = CapitalViewGraphData[4];
}
//ParentView
BoxPlotSeries parentViewSeriesData = null;
if (ParentViewGraphData != null && ParentViewGraphData.Length >= 5)
{
parentViewSeriesData = new BoxPlotSeries();
parentViewSeriesData.color = "#C111A0";
parentViewSeriesData.name = "Parent Company";
parentViewSeriesData.data = new List<Data>();
parentViewSeriesData.data[0].Low = CapitalViewGraphData[0];
parentViewSeriesData.data[0].Q1 = CapitalViewGraphData[1];
parentViewSeriesData.data[0].Median = CapitalViewGraphData[2];
parentViewSeriesData.data[0].Q3 = CapitalViewGraphData[3];
parentViewSeriesData.data[0].High = CapitalViewGraphData[4];
}
return new SeriesGeneric<BoxPlotSeries>
{
Data = new List<BoxPlotSeries> { captiveViewSeriesData, parentViewSeriesData }
};
}
}
Upvotes: 0
Views: 84
Reputation: 119066
You're trying to access a specific index of a list that is empty. You need to add a new item instead. For example:
captiveViewSeriesData = new BoxPlotSeries();
captiveViewSeriesData.color = "#FFB81C";
captiveViewSeriesData.name = "Captive";
var data = new Data
{
Low = CapitalViewGraphData[0],
Q1 = CapitalViewGraphData[1],
Median = CapitalViewGraphData[2],
Q3 = CapitalViewGraphData[3],
High = CapitalViewGraphData[4]
};
captiveViewSeriesData.data = new List<Data>();
captiveViewSeriesData.data.Add(data);
Upvotes: 1