Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics (Retired) Perfect Final

Takuya Shinozaki
Takuya Shinozaki
1,149 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
225,730 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.