0

I'm new to C# and having an error where I need to have the DataPoints array to return empty but I'm getting this error for whatever reason. What am I doing wrong here?

Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"

            var DataPoints = new DataPoints();
            // Generate DataPoints (or something similar) from the newly constructed WspViewList.
            foreach (WspViewRow row in dataPointBuilder)
            {
            
            var dataPointList = row.OriginalData.TrimStart('[').TrimEnd(']').Split(',').ToList();

            DataPoints.labels.Add(dataPointList[1]);

            for (var index = 2; index< dataPointList.Count; index++)
            {
                var dataPoint = dataPointList[index];
                if (string.IsNullOrEmpty(dataPoint))
                    continue;
                ChartDataObject cdo;
                if (DataPoints.datasets.Count <= index - 2)
                {
                    cdo = new ChartDataObject();
                    DataPoints.datasets.Add(cdo);
                    cdo.label = ColumnObjects[index].propertyName;
                }
                else
                    cdo = DataPoints.datasets[index - 2];

                cdo.data.Add(dataPoint);
                DataPoints.datasets[index - 2] = cdo;
            }
            
                DataPointAPI DataPointResponse = new DataPointAPI()
                {

                data = DataPoints,

                };

                dataset.Add(DataPointResponse);
            }

        // Set some class field to contain these datapoints
        ChartData = dataset;
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
kjamp
  • 365
  • 3
  • 10
  • 37
  • It looks like you are processing a JSON as plain string instead of deserializing it. That would probably be a good way to prevent that error. – VDWWD Oct 26 '20 at 21:26
  • how would I fix that? I'm new to C# so I'm lost at the syntax – kjamp Oct 26 '20 at 21:27
  • Take a look at https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c – VDWWD Oct 26 '20 at 21:28
  • On this line: `for (var index = 2;`, you're starting `index` at `2` without first validating that there are more than 2 items in the array. Then when you get here: `var dataPoint = dataPointList[index];`, you get the exception. To solve this, check how many items are in the array before accessing a hard-coded index. – Rufus L Oct 27 '20 at 00:15
  • That sounds correct @RufusL, im lost how to check how many items and what that looks like. I'm completely new with the syntax so I understand but still a little lost – kjamp Oct 27 '20 at 02:19
  • Arrays have a `Length` property that tells you how many items it can hold. The largest index is `Length - 1`, since arrays are zero-based – Rufus L Oct 27 '20 at 03:06
  • so what should I be doing here? making an if conditional? if so what does that look like? – kjamp Oct 27 '20 at 04:15
  • what I want to do is return an empty array to the api is no items are passed though – kjamp Oct 27 '20 at 04:34

1 Answers1

0

When you get the exception, you could get the line number where it happend. You can also use debbuger to see if you miss something.

Actually your exception is throwed by trying to accessing data outsite your array / list. Remember that your index is starting from 0 to total length of your array - 1

var array = new []{1,2,3};
array[0] => 1
array[1] => 2
array[2] => 3
array[3] => out of range
Orkad
  • 630
  • 5
  • 15
  • How would I fix this in my code? I’m honestly lost on where I would fix that issue – kjamp Oct 26 '20 at 21:11
  • what I want to do is return an empty array to the api is no items are passed though – kjamp Oct 27 '20 at 04:34
  • You can compare the length of your array with .Count / Length / Any() in a if, then you can use the "continue" keyword to get to the next iteration. – Orkad Oct 27 '20 at 06:05