Thread: Array Help
View Single Post
  #1 (permalink)  
Old 04-24-2008, 02:00 PM
peage1475's Avatar   
peage1475 peage1475 is offline
Newbie
 
Join Date: Apr 2008
Posts: 5
Credits: 10
Rep Power: 0
peage1475 is on a distinguished road
Default Array Help

Hello,

I am trying to write a program for a class that I am taking and I am having a little trouble with assigning values to an array. Here is the entirety of my code the highlighted area is where I am having the problem:

Code:
 
#region Using Directories
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
#endregion

namespace Programming_Project_Text_Query
{
    public class Program
    {
        static void Main()
        {
            string fileName;
            Console.WriteLine("Insert the text file name.");
            fileName = Console.ReadLine();
            try
            {
                StreamReader checkFile = new StreamReader(fileName);
                Concordance newIndex = new Concordance(fileName);
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File not found, please try again.");
                Console.WriteLine("\n");
                Main();
            }
        }
    }
    public class Concordance//: IDictionary   
    {
        public static StreamReader file;
        public Concordance(string f)
        {
            Dictionary<string, int[]> Concordance = new Dictionary<string, int[]>();
            FileInfo newFile = new FileInfo(f);
            file = newFile.OpenText();
            string[][] fileByWord = new string[findNumberOfLines()][];
            int L = 0;
            using (StreamReader file2 = newFile.OpenText())
            {
                string line = "";
                while ((line = file2.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                    string[] tempArray = new string[(line.Split(' ')).Length];
                    tempArray = line.Split(' ');
                    int arrayLength = tempArray.Length;
                    for (int W = 0; W < tempArray.Length; W++)
                    {
                        fileByWord[L][W] = tempArray[W];
                    }
                    L++;
                }
            }
        }

        #region findNumberOfLines() Method
        public static int findNumberOfLines()
        {
            int i = 0;
            string line = file.ReadLine();
            while (line != null)
            {
                i++;
                line = file.ReadLine();
            }
            return i;
        }
        #endregion
    }
}
When the program is run in debug mode the red colored code is highlighted and brings up the NullReferenceException was unhandled. Object reference not set to an instance of an object. Can anyone help me fix this becasue I have no idea what I did worng, and everything I try does not work.
Thanks

Last edited by peage1475; 04-24-2008 at 02:23 PM.
Reply With Quote

Sponsored Links