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! While you're at it, check out some resources Treehouse students have shared here.

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

I get a System.NullReferenceException when I attempt the challenge. It works when I copy the code over to the workspace?

I can't figure it out. The program compiles and works as planned in Workspaces.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {


            while (true)
            {
                Console.Write("Enter the number of times to print \"Yay!\": ");
                var times = Console.ReadLine();
                    if (times.ToLower() == "quit")
                    {
                    Console.WriteLine("Goodbye!");
                    break;
                    }
                try
                {
                int loopRuns = int.Parse(times);

                        if (loopRuns <= 0)
                        {
                         Console.WriteLine("You entered an invalid number");
                         continue;
                        }
                        else
                        {



                            while(true)
                            {
                                if (loopRuns <= 0)
                                {
                                break;
                                }
                                else
                                {
                                Console.WriteLine("Yay!");
                                }
                                loopRuns = loopRuns - 1 ;
                            }
                        }
                 }
                 catch(FormatException)
                 {
                   Console.WriteLine("That is not valid input.");
                   continue;
                 }



            } 
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,248 Points

The challenges have specific objectives beyond just making code that compiles.

You have an outer loop that prevents the program from ending. 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 loop it never ends.