C# C# Basics Perfect Final

Takuya Shinozaki
Takuya Shinozaki
1,060 Points

System.NullReferenceException

I'd like to know how to fix this error. I already view other question's answer regarding this code challenge, and I know that there is better way of coding to this challenge. But I would like to know is how to avoid this error that says System.NullReferenceException . When I tried this code on my workspace, it ran fine and Program did everything what I wanted it to do.But when I type this code to the box in code challenge page, error occurs.

I appreciate if you can determine what is wrong with this code.

Program.cs
using System;
namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
          { bool programstarter = true;
       while(programstarter)
       {  bool Keepgoing = true;
        var z = 0;
         Console.Write("Enter the number of times to print \"Yay!\":");
           var x = Console.ReadLine();
        if(x.ToLower() == "quit")
                 {break;}
        else
        {
        while(Keepgoing)
           { 
                 try
                 {var y = int.Parse(x);
                  if (y <=0)
                  {Console.WriteLine("The number you've entered is invalid");
                   Keepgoing = false;
                   continue;}
                  else
                  {
                   Console.WriteLine("Yay!");
                   z=z+1;
                  }
                   if ( z == y )
                   {
                       Keepgoing = false;
                     continue;
                   }
                   else
                   {continue;
                   }
                 }

                 catch(FormatException)
                 {Console.WriteLine("This is an invalid entry");
                 Keepgoing = false;
                   continue;}
               catch(ArgumentNullException)
                   {Console.WriteLine("This is an invalid entry");
                 Keepgoing = false;
                   continue;}
               catch(OverflowException)
                   {Console.WriteLine("This is an invalid entry");
                 Keepgoing = false;
                   continue;}
                }
        }
       continue; }
          }
}
}

1 Answer

Steven Parker
Steven Parker
85,422 Points

This challenge only requires one loop, but I see you have three nested loops.

A program built according to the instructions will prompt and accept input one time, and based on the input it will either print "Yay!"s or an error message. Either way, it should end. With the extra loops it never ends.