How to handle null values in C#. When I am entering characters in a char array i want to leave some values blank or empty (null). But when i do that my program terminates giving me an error. The error is shown in the pic below. What should i do to handle this exception. Please help!!!!

null_error

Showing your code would be more helpful than just the output.

What should i do to handle this exception.

Don't cause it in the first place. If values can be null, be sure to check for null when doing things like output and use a safe alternative.

This is the full code:

 class Program
    {
        static void Main(string[] args)
        {
            char[] array = new char[20];
            //char[] array1 = new char[20];
            Console.WriteLine("Enter your answers Choice: A,B,C,D");
            driverexam d = new driverexam();
              d.input(array);
              d.passed(array);
              d.totalcorrect(array);
              d.totalincorrect(array);
              d.questionmissed(array);



        }
    }

    class driverexam
    {
        char[] answer = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A' };
        int i,count=0,wrong=0,missed=0;
        public void input(char[] array)
        {
            if (array != null)
            {
                for (i = 0; i < array.Length; i++)
                {
                    array[i] = Convert.ToChar(Console.ReadLine());
                }
            }

        }

        public void passed(char[] array)
        {




            for (i = 0; i < array.Length; i++)
            {
                if (array[i] == answer[i])
                {
                    count++;
                }

            }

            if (count >= 15)
            {
                Console.WriteLine("YOU HAVE PASSED!!!!!!!!");
            }
            else
                Console.WriteLine("YOU HAVE FAILED!!!!!!!!");

        }

        public void totalcorrect(char[] array)
        {
            count = 0;
            for (i = 0; i < array.Length; i++)
            {
                if (array[i] == answer[i])
                {
                    count++;
                }
            }

            Console.WriteLine("Total Correct : {0}", count);
        }

        public void totalincorrect(char[] array)
        {
            for (i = 0; i < array.Length; i++)
            {
                if (array[i] != answer[i])
                {
                    wrong++;
                }
            }

            Console.WriteLine("Total InCorrect : {0}", wrong);

        }

        public void questionmissed(char[] array)
        {
            missed = 0;
           for (i = 0; i < array.Length; i++)
            {
               if (char.IsWhiteSpace(array[i]))
               {
                    missed++;

               }
           }

           Console.WriteLine("Total Questions : {0}", missed);
        }

    }
}

It works fine if I dont leave any value as blank but when i leave a value empty or blank then my program terminates and i get the error stated as above. How should i handle such exceptions cause i want to calculate the no of questions missed?

On line 30, if you just tap return for a test with no answer you will get a null value from ReadLine, wich you are trying to convert to a char wich gives your error.
So test for null values( as deceptikon proposes) before you do the conversion to a char.
Maybe the simplest way is to use a dummy char ( a space, an 'X' etc.) for a test not answered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.