Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Final

tmontez
tmontez
3,333 Points

My code for this challenge works in my workspace. I get a NullReferenceException here. Why doesnt it work here?

Here is my code:

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() { while(true) { Console.Write("Enter the number of times to print \"Yay!\": "); string entry = Console.ReadLine();

             if(entry.ToLower() == "q")
             {
                break;
             }

            try
            {
                int count = int.Parse(entry);

                if(count <= 0 )
                {
                  Console.WriteLine("Enter a number greater than 0 or type \"q\" to quit.");
                  continue;
                }

                while(count > 0)
                {
                    Console.WriteLine("Yay!");
                    count -= 1;
                }
                break;

            }
            catch(FormatException)
            {
                Console.WriteLine("Invalid Input! Please enter a number greater than 0 to try again or type \"q\" to quit.");
            }
        }
        Console.WriteLine("See Ya!");
    }
}

}

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            while(true)
            {
                Console.Write("Enter the number of times to print \"Yay!\": ");
                string entry = Console.ReadLine();

                 if(entry.ToLower() == "q")
                 {
                    break;
                 }

                try
                {
                    int count = int.Parse(entry);

                    if(count <= 0 )
                    {
                      Console.WriteLine("Enter a number greater than 0 or type \"q\" to quit.");
                      continue;
                    }

                    while(count > 0)
                    {
                        Console.WriteLine("Yay!");
                        count -= 1;
                    }
                    break;

                }
                catch(FormatException)
                {
                    Console.WriteLine("Invalid Input! Please enter a number greater than 0 to try again or type \"q\" to quit.");
                }
            }
            Console.WriteLine("See Ya!");
        }
    }
}

1 Answer

Steven Parker
Steven Parker
221,500 Points

This issue comes up pretty regularly. I think the best hints are in this post from 6 months ago (nobody ever scans the forum archives). For your convenience, I'll repeat the content here, with a slight enhancement:


People often over-think this one and get too creative, and their answer is rejected for a variety of reasons (some which don't seem to make any sense).

Here are the secret "rules" you must follow for success on this one:

  • Your program must ask for and accept input ONE TIME ONLY (so no looping, an no "exit" provision)
  • If the input validates, your program will perform exactly as in Task 1
  • If the input does not validate, it will print the error message and exit
  • Validation must be done by exception catching. There are other perfectly good methods that can be used for validation, but they will not pass this challenge