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 trialBernt Erik Engebretsen
5,377 PointsI get this error message:'System.ArgumentNullException: Value cannot be null. Parameter name: String. See output for sta
What is wrong with the code?
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
bool notCorrectInput = true;
int numberTimes;
while (notCorrectInput)
{
try
{
Console.Write("Enter the number of times to print \"Yay!\": ");
string numTimes = Console.ReadLine();
numberTimes = int.Parse(numTimes);
int counter = 0;
while (counter < numberTimes)
{
Console.WriteLine("\"Yay!\"");
counter += 1;
}
}
catch (FormatException)
{
Console.WriteLine("You must enter a whole number.");
continue;
}
notCorrectInput = false;
}
}
}
}
2 Answers
Steven Parker
231,236 PointsI've answered this one before, for example in "C# Basics, Final Challenge, Task 2 of 3". Well, anyway, here it is again:
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 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 violates rule 1 and 3. Just remove the loop.
Bernt Erik Engebretsen
5,377 PointsThank you very much! I learned a lot from making the mistakes and in making my code simpler. Bernt
Christian Barahona
1,354 PointsChristian Barahona
1,354 PointsThank you for the clarification!!!