Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i write a simple code but i have an strange exception .

int[] vertex;
        int[,] AMatrix;
        int[,] AdjacencyMatrix;
        int Length;

        public void SetVertex()
        {
            Console.WriteLine("Please enter the number of vertices");
            int n = Convert.ToInt32(Console.ReadLine());
            while (n < 2)
            {
                Console.WriteLine("You must enter a number greater than 1\n plz enter another one:");
                n = Convert.ToInt32(Console.ReadLine());
            }
            vertex = new int[n];
            for (int i = 0; i <= n-1; i++)
                vertex[i] = i;
        }
        public void SetEdges()
        {
            AdjacencyMatrix = new int[vertex.Length, vertex.Length];
            for (int i = 0; i < vertex.Length; i++)
                for (int j = 0; j < vertex.Length; j++)
                    AdjacencyMatrix[i, j] = 0;

            Console.WriteLine("please enter the number of the edges:");
            int e = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < e; i++)
            {
                Console.WriteLine("from ");
                int s = 0;
                s = Convert.ToInt32(Console.ReadLine());
                   
                Console.WriteLine("to ");
                int d = 0;
                d = Convert.ToInt32(Console.ReadLine());
                AdjacencyMatrix[s, d] = 1; // <<-------- Exception happens here
            
        }

in line that i bold it i have an exception i cant understand, please help me to remove this exception :(
Posted
Updated 30-Jan-10 1:23am
v3

The code seems to be fine,

Let me know what error you are getting.

Is it IndexOutOfRangeException?

If this is so, then you are setting a value to an array which is not allocated.

:)
 
Share this answer
 
Since the exception happens at the line indicated, I would think that the user entered a digit that was out of range of the array. Since there are two values, and you didn't give us the values input by the user, it's impossible to say which is the bad one.

It is, however, the most probably cause. Christian mentioned that it could have been a non-numeric character entered by mistake, but that would have thrown an exception where you called Convert.ToInt32.

For what it's worth, your code is pretty bad. NEVER use one-character variable names for anything more important than a loop controller (even if this is just a homework assignment). I would also use a List of objects instead of an doubly-subscripted array of integers. Take the text-speak pout of your command prompts, and tell the user what the valid values are when prompting him for them. If you're going to learn how to write code, learn how to write it correctly. Finally, add some comments.
 
Share this answer
 
I guess you don't really want help, or you'd have told us what the exception was.

elahe_eli wrote:
n = Convert.ToInt32(Console.ReadLine());


This will also throw an exception if a non number is entered.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900