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

Why does this code throw System.ArgumentNullException:

This code works perfectly in workspaces, it throws no exceptions and catches the exception. why wont it pass?

Program.cs
using System;

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

                    int answer = int.Parse(Console.ReadLine());
                    int count = 0;

                    while (answer > count)
                    {
                        Console.WriteLine("Yay!");
                        count ++;
                    }
                }
                catch(FormatException)
                {
                    Console.WriteLine("Input is invalid");
                    continue;
                }
                break;
            }

        }
    }
}

3 Answers

Steven Parker
Steven Parker
231,236 Points

:point_right: Your program "works", but deviates from the instructions.

This is a great example of how something that works (will compile an run) might not be what was asked for (satisfies the requirements). This has been a topic of many previous questions, but I'll repost the info here:

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
  • If the input validates, your program will perform exactly as in Task 1
  • If the input does not validate, it will print the specified 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

It looks like you have a loop that deviates from the first requirement. Your error message is also different from the specified "You must enter a whole number."

Thanks Steven, I understand now how to pass this exercise but I still think a couple of things don't add it up. Firstly why does asking for the input again if they enter something that catches a formatexception cause the exercise to fail? There is nowhere where it says ask ONE time only. Secondly why was treehouse saying there was a argumentnullException when the compiler threw no exceptions?

Steven Parker
Steven Parker
231,236 Points

You're right, the instructions never explicitly say "one time only" — that's part of my coalescing the essential requirements as implied by the sequence of instructions given from task to task into the hints I provided..

And the specific error you saw may not have been evident from the code, but the checking process sometimes involves unseen modifications that can produce errors when the submitted code is significantly different from the expectations.

If you consider these points to be serious flaws in the challenge, I encourage you to submit your comments to the staff on the Support page.