0

I'm beginner in programming and I have tried some simple code in C#. [1]: https://i.sstatic.net/zLVbz.jpg This code deals with simple array initialization ,storing and sorting and so on.Its runs as usual in the first try but when I again want to store in the array it throws the exception which I don't understand. [![Its the exception I'm getting][1]][1]

static void Main(string[] args)
{
    int number;
    char y;

    string[] answer = new string[10];
    bool keeprompting = true;
    while (keeprompting)
    {
        Console.WriteLine("Enter the options given below 1.Add students\n 2.View all details\n 3.Sorting\n 4.Exit\n");
        int input = Convert.ToInt16(Console.ReadLine());

        switch (input) {
            case 1:
                Console.WriteLine("Enter the Number of Students to be added to the List");
                number = Convert.ToInt16(Console.ReadLine());  **exception **
                for (int i = 0; i < number; i++) {
                    answer[i] = Console.ReadLine();
                }
                break;

            case 2:
                foreach(var item in answer)
                {
                    Console.WriteLine(item.ToString());
                }
            break;
            case 3:
                Array.Sort(answer);
                foreach(var item in answer)
                {
                    Console.WriteLine(item.ToString());  **exception **
                }
            break;

            case 4:
                Console.WriteLine("Are you sure you want to exit");
                Console.WriteLine("1 for Yes and  for No");
                y = (char) Console.Read();
                if (y != 1) {
                    keeprompting = true;
                } else {
                    keeprompting = false;
                }
                Console.WriteLine("thank you");

                break;
        }
    }
}

Any and all suggestion are welcome.

1
  • Input string not in correct format means the text entered was not an integer. Better to use Int16.TryParse. Commented Jan 24, 2016 at 7:50

2 Answers 2

1

1) Possibly FormatException raised here int input = Convert.ToInt16(Console.ReadLine()); because you enter to the Console not '1', '2', etc., but '1.', '2.' and so on. Or may be you use other symbols that cannot be parsed using Convert.ToInt16() method. Value you enter should be in range -32768..32767 and should not contain any space, dot, comma and other symbols except minus sign.

2) The same may happen here number = Convert.ToInt16(Console.ReadLine()); **exception**

3) And here I think you get NullReferenceException:

Array.Sort(answer);
foreach (var item in answer)
{
    Console.WriteLine(item.ToString());  **exception**
}

It happens because you have 10 items in your array (string[] answer = new string[10];), but when you insert students you may insert less than 10, so you have for exapmle 3 items initialized, and other set to its default value - null for string. So your array looks like this: { "John", "Jack", "Ben", null, null, ..., null }. A foreach statement later iterates each item including null values, and it tries to call method on a null object, so you get NullReferenceException.
May be you should better use List<string> instead of array to avoid such kind of problems. Call Add() to add items, and Clear() method before you enter students again(to get free of students from previous "session"). And when you later iterate this collection with a foreach loop everything will be ok.

Sign up to request clarification or add additional context in comments.

1 Comment

Its like basic assignment so I thought of using array
1

I posted 2 solutions in your other question about this method in this program that accounts for the array size issues in two different ways. Use int.TryParse instead of convert and you either need to resize the array based on user input on the number of students or you need to check for nulls in each iteration of the answer array, skipping nulls. See the example code i provided in your other question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.