0

So for some reason I seem to be getting a null value on line 43 and im not quite sure why that is. Can someone please explain the issue to me?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

    namespace ConsoleApplication9
    {
        class Program
        {
            static void Main(string[] args)
            {

        string myGradesStrg;
        string[] gradesStrgArr = new string[50];
        int[] gradesArray = new int[50];

            string environment = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\";

                string path = environment + "Grades";

            StreamReader gradesFile = new StreamReader((@"C:\Users\Soto\Documents\Grades.txt"));

            do
                {
                    myGradesStrg = gradesFile.ReadLine();

                    if (myGradesStrg != null)
                    {
                        Console.WriteLine(myGradesStrg);

                        int i = 0;
                        gradesStrgArr[i] = myGradesStrg;
                        i++;

                    }
                } while (myGradesStrg != null);

            Console.ReadLine();

This is line 43:

            gradesArray = Array.ConvertAll(gradesStrgArr, int.Parse);

This is the error: An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll

Additional information: Value cannot be null.

            decimal myAvgGrade = MyGradeAverage(gradesArray);

            Console.WriteLine("The average of the grades is {0:0}", myAvgGrade);

            Console.ReadLine();

    }

    // The MyGradeAverage Method
    // Purpose: Calculates the average of a given array
    // Parameters: an integer array list
    // Returns: the average of a given array 
    static decimal MyGradeAverage(int[] myArray)
    {
        decimal average =(decimal) myArray.Average();
        return average;

    }
}
Soto
  • 15
  • 1
  • 7
  • 3
    You have declared an array of strings that can hold 50 strings. Did you fill every slot available with a string? If not then you have your nulls – Steve Apr 18 '15 at 12:11
  • add a debug output just before that line and dump out the content of the array. you will find nulls inside.. – Michael Sander Apr 18 '15 at 12:13
  • be sure you have initialized all cells with an integer value such as 0 – Mahmoud Apr 18 '15 at 12:13
  • Well this assignment I have specifically states to create an integer array with 50 values, yet the file we are given to work with has only about 10 values. I had assumed I should do the same with the strings but only because I didn't know any better way to do it. So I know how to get rid of the nulls but how should my string array be declared? – Soto Apr 18 '15 at 12:17

0 Answers0