I'm new at using Parallel so probably I'm missing something. When running this code using Parallel.For
I get a NullReferenceExcempion. When doing exactly the same thing with normal for it works perfectly.
I've found other people asking the same question but in their cases the problem was always that they were declaring the variable outside the for scope which of course can generates all sorts of messy things. I've found no people experiencing this issue as I'm experiencing it.
In my case the variable is declared inside... how can I get a null reference?
Parallel.For(0, Outer.ThisValidation.RawData.RawDataStorage.Count, i =>
{
//This line gets the NullReference.
//I'm declaring and assigning the variable in the same line, how on Earth can ThisSer throw a NullReferenceException
Series ThisSer = chartEquitiesBySymbol.Series.Add(Outer.ThisValidation.RawData.RawDataStorage[i].StringedIndex);
ThisSer.IsVisibleInLegend = false;
ThisSer.ChartType = SeriesChartType.Line;
ThisSer.BorderWidth = 2;
for (int j = 0; j < Outer.ThisValidation.RawData.RawDataStorage[i].EquityData.EquityLine.Count; j++)
ThisSer.Points.AddXY(Outer.ThisValidation.RawData.RawDataStorage[i].EquityData.EquityLine[j].Date, Outer.ThisValidation.RawData.RawDataStorage[i].EquityData.EquityLine[j].Value);
Outer.ThisValidation.RawData.RawDataStorage[i].AddSeries(WindowID, ThisSer);
});
The error is shown in the comment. I'm adding a series to a chart, the reference of the series is immediately stored in an object to be able to do my stuff with it later on. I get the null reference exception at the assigning stage. Usally it blocks after a variable number of iterations between 5 and 15.
Here is what happen:
The row with the red X is throwing the exception. The tooltip shows the null value at assigning stage.