0

I am receiving a null pointer exception, and I narrowed it down to this small block of code. I think it is because when I set teamStats[iterator].median = Statistics.median that it can't because it isn't initialized yet. Though I don't know the proper way to initialize an array of objects or the memory ramifications that this code will cause.

Below is the majority of the code i am dealing with besides the class describing TeamStat, but I've used all of its members below and they are public double.

        TeamStat[] teamStats = new TeamStat[DistCount+1];
        int iterator = 0;
        foreach (int i in TeamIDlist)
        {
            var p = userList.Where(x => x.TeamId.Equals(i)).Select(y => (double)y.BS).ToArray();

            var statistics = new DescriptiveStatistics(p);
            teamStats[iterator].median = Statistics.Median(p);

            teamStats[iterator].largestElement = statistics.Maximum;
            teamStats[iterator].smallestElement = statistics.Minimum;
            teamStats[iterator].mean = statistics.Mean;
            teamStats[iterator].variance = statistics.Variance;
            teamStats[iterator].stdDev = statistics.StandardDeviation;
            iterator++;
        }

Update

Is this the correct way to do this:

            TeamStat[] teamStats = new TeamStat[DistCount];
        int iterator = 0;
        foreach (int i in TeamIDlist)
        {
            //Added these two lines
            TeamStat temp = new TeamStat();
            teamStats[iterator] = temp;


            var p = userList.Where(x => x.TeamId.Equals(i)).Select(y => (double)y.BS).ToArray();

            var statistics = new DescriptiveStatistics(p);
            teamStats[iterator].median = Statistics.Median(p);

            teamStats[iterator].largestElement = statistics.Maximum;
            teamStats[iterator].smallestElement = statistics.Minimum;
            teamStats[iterator].mean = statistics.Mean;
            teamStats[iterator].variance = statistics.Variance;
            teamStats[iterator].stdDev = statistics.StandardDeviation;
            iterator++;
        }
schumacherj
  • 1,284
  • 5
  • 18
  • 33
  • You can put it in one line (`teamStats[iterator] = new TeamStat();`), but, yes, that's the correct way. – Heinzi Mar 26 '14 at 06:38

2 Answers2

1

Here

TeamStat[] teamStats = new TeamStat[DistCount+1];

you initialize the array. At this moment, the array contains DistCount + 1 null entries.

If you want the array to contain DistCount + 1 new TeamStat entries, you need to initialize them in a loop:

TeamStat[] teamStats = new TeamStat[DistCount+1];
for (var i = 0; i < DistCount + 1; i++)
    teamStats[i] = new TeamStat();
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • That's just what i got in the end. I didn't know if it was a bad way of doing it though. Thank you. – schumacherj Mar 26 '14 at 06:35
  • 1
    @schumacherj: It's the correct way. There is no built-in way to automatically initialize an array of objects, see [this question](http://stackoverflow.com/q/4839470/87698) for details. – Heinzi Mar 26 '14 at 06:36
0

Your code has 2 issues 1) Each object in Array needs to be initialized before accseing its properties teamStats[iterator] = new TeamStat(); 2) You need to make sure you initizliaed array with same/more capacity as you are trying to assign values to.(TeamIDlist < DistCount+1), otheriwse it will throw index out of range exception.

Prinshul Jain
  • 116
  • 1
  • 6