0

Is someone able to help me out with the following please, I'm trying to split data from an input file (2 pieces of data per line, separated by either of the delimiters specified in the code below). To do this I have declared the string array 'split input', however when I run the program I get a runtime error (screenshot) with the split input line inside the while loop highlighted in yellow. I can't see what I am doing wrong, I'm copying sample code which seems to be working fine :( NB - the messageBox line below the yellow is just for my testing to prove the split worked

        private int DetermineArraySize(StreamReader inputFile)
    {
        int count = 0;
        while (!inputFile.EndOfStream)
        {
            inputFile.ReadLine();
            count++;
        }
        return count;
    }

    private void ReadIntoArray(StreamReader inputFile, string[] gameArray, int[] revArray)
    {
        string rawInput;
        string[] splitInput = new string[2];
        int count = 0;
        char[] delimiters = {'=', '@',};

        while (!inputFile.EndOfStream || count < gameArray.Length)
        {
            rawInput = inputFile.ReadLine();
            {
                splitInput = rawInput.Split(delimiters);
                MessageBox.Show(splitInput[0] + " // " + splitInput[1]);

                count++;
            }

        }

    }

    private void rdGameSalesForm_Load(object sender, EventArgs e)
    {

        StreamReader inputFile = File.OpenText("GameSales.txt");    //Open Input File
        int arraySize = DetermineArraySize(inputFile);              //Use input file to determine array size
        string[] gameTitle = new string[arraySize];                 //Declare array for GameTitle
        int[] revenue = new int[arraySize];                         ///Declare array for Revenue

        ReadIntoArray(inputFile, gameTitle, revenue);

Thanks for your help

Rick D
  • 139
  • 1
  • 11
  • Use debug and see what is null in your code ! I suppose your count < gameArray.Length, but your file is in the end of the stream and rawInput is null. – mybirthname Nov 11 '16 at 06:44
  • 1
    The call to ReadLine returns null at least once before the EndOfStream. – Zohar Peled Nov 11 '16 at 06:45
  • 1
    why do you check `count < gameArray.Length`? count is always 0 – M.kazem Akhgary Nov 11 '16 at 06:52
  • I hadn't added 'count++ line' below the messagebox line as I wanted to see if the file was being read before I continued. I have added it now but I'm still getting same error. It doesn't look like the input file is being read. I have updated my original post with extra code so you can see where I am passing the input file to to the method as an argument – Rick D Nov 11 '16 at 07:09

2 Answers2

1

Just add check on null.

ReadLine Method returns null if the end of the input stream is reached. It is possible because you check !inputFile.EndOfStream or count < gameArray.Length. So in the second condition has a possibility to get null when input filre reading

 while (!inputFile.EndOfStream || count < gameArray.Length)
        {
            rawInput = inputFile.ReadLine();
            if(rawInput !=null)
            {
              splitInput = rawInput.Split(delimiters);
              MessageBox.Show(splitInput[0] + " // " + splitInput[1]); 
            }
        }
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • That has worked to get rid of the error, but I don't understand why 'rawinput' is null, it should be reading from the text file I passed as an argument when calling the method? – Rick D Nov 11 '16 at 06:56
  • Because you read file line by line and if it is the end of file it's still reading one more line because `count < gameArray.Length` is passed – Roman Marusyk Nov 11 '16 at 06:59
0

Check for null instead of end of stream.

 while((rawInput = Inputfile.ReadLine()) != null)
 {
     splitInput = rawInput.Split(delimiters);
     MessageBox.Show(...);
 }
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • wouldn't it break the loop if an empty line is in between two non empty lines? In that case this won't be a proper solution!! – Aamir Masood Nov 11 '16 at 06:54