0

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:

Exception Screenshot

The row with the red X is throwing the exception. The tooltip shows the null value at assigning stage.

  • Debug your code and inspect the variables. One of them is null. It's impossible to guess which one it is just by looking at the code – Panagiotis Kanavos Apr 04 '18 at 16:03
  • BTW modifying a collection in parallel will result in *bugs* unless the collection is thread-safe. You can't modify UI controls from other threads either. You *won't* get better performance by trying to add points to a chart this way. All UI controls support data binding. Bind your data to your control instead of adding points one by one – Panagiotis Kanavos Apr 04 '18 at 16:06
  • What is `chartEquitiesBySymbol`? A third-party control, your own UI class? Why are you trying to load points in parallel anyway? You *don't* need that to display stock info. – Panagiotis Kanavos Apr 04 '18 at 16:08

1 Answers1

-1

The NullReferenceException comes not from the fact that ThisSer is null.
Something in this expression is null:

chartEquitiesBySymbol.Series.Add(Outer.ThisValidation.RawData.RawDataStorage[i].StringedIndex);

That's what you need to figure out.

Because the exception is thrown in the right hand side of the assignment, naturally, the assignment target remains null.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443