Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C# Programming

C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-24-2008, 02:00 PM
peage1475 peage1475 is offline
Newbie
 
Join Date: Apr 2008
Posts: 2
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-24-2008, 07:41 PM
gaylo565's Avatar   
gaylo565 gaylo565 is online now
Learning Programmer
 
Join Date: May 2007
Location: flagstaff, az
Posts: 97
Last Blog:
String Manipulation wi...
Rep Power: 6
gaylo565 will become famous soon enoughgaylo565 will become famous soon enough
Default Re: Array Help

I think your problem is that the array is never set to a static number of rows. You can have a dynamic array but you must have an initial value when you start using the array.

" string[][] fileByWord = new string[findNumberOfLines()][];"

where u instantiate the array u can put a 0 for the # of rows and then it will set its own value as you add records...it should end up largest split length that the file has...I hope this helps some and someone let me know if i'm way off
ps...i didnt try to see if it worked cuz i have VS 2003 wich doesn't contain the collections.generic namespace.

another thing is u set an int for the array length but don't use it in your for statement. I dont think this is responsible for the error message your getting but it could cause another 1

Last edited by gaylo565; 04-24-2008 at 07:51 PM. Reason: afterthought
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-24-2008, 08:25 PM
peage1475 peage1475 is offline
Newbie
 
Join Date: Apr 2008
Posts: 2
Rep Power: 0
peage1475 is on a distinguished road
Default Re: Array Help

yeah that was the problem, and i fixed it. thanks for your help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-25-2008, 11:14 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 3,471
Last Blog:
Web slideshow in JavaS...
Rep Power: 30
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: Array Help

Well done, gaylo565. +rep given.
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Array manipulation Question in C++ JJJIrish05 C and C++ 1 03-08-2008 02:06 PM
Help regarding the sorting of arrays with flags! Yuriy M C and C++ 3 10-12-2007 10:30 PM
Usage of array structures to increment letter instances of text Yuriy M C and C++ 2 09-13-2007 10:49 AM
fread into array position kenna C and C++ 0 08-17-2007 08:03 AM
Python 2D array question annannienann Python 3 04-23-2007 04:36 PM


All times are GMT -5. The time now is 10:35 AM.

Contest Stats

Xav ........ 164.00000
dargueta ........ 128.00000
John ........ 127.00000
gaylo565 ........ 18.00000
XaNaX ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

Ads