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

Fixing an ArgumentNullException error

I originally received a compiler error stating there is a ArgumentNullException error in my String parameter. I attempted to resolve this error by adding a second "catch" clause. However, when I check my work again it errors out but time time with no explanation. So now I'm not sure what is wrong with my code.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            int timesToPrint = 0;
            while(true)
            {
                var countString = Console.ReadLine();
                try 
                {
                    timesToPrint = int.Parse(countString);

                    if(timesToPrint < 0)
                        Console.WriteLine("You entered a negative value.");

                    // Print Yay!
                    while(timesToPrint > 0)
                    {
                        Console.WriteLine("Yay" + " ");
                        timesToPrint--;
                    }
                    break;
                }
                catch(FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                    continue;
                }
                catch(ArgumentNullException)
                {
                    Console.WriteLine("You must enter a response.");
                    continue;
                }
            }   

        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,248 Points

It looks like you have an extra loop.

This challenge only needs one loop to print out the right number of "Yay!" messages. It looks like you have an extra outer loop that's not needed and causing the program to require input more than once.

Also, remember the exclamation point in "Yay!".