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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Tristan Kasinger](https://secure.gravatar.com/avatar/164652cb3cd79104753944517b61f513?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Tristan Kasinger
919 PointsSystem.NullReferenceException
Snapshot:
It runs a-okay in the workshop window but when I paste it into the submission space for the final, I get this error. How do I simplify my code or whatever needs to be done in order to pass the final?
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
int timesYay = 0;
bool keepGoing = true;
while(keepGoing)
{
Console.Write("Enter the number of times to print \"Yay!\": ");
string entry = Console.ReadLine();
if (entry.ToLower() == "quit")
{
break;
}
bool yayLoop = true;
while (yayLoop)
{
timesYay = timesYay + 1;
if (timesYay == (int.Parse(entry) + 1))
{
yayLoop = !yayLoop;
break;
}
else
{
Console.Write("Yay!");
}
}
}
}
}
}
2 Answers
![Chanuka Weerasinghe](https://uploads.teamtreehouse.com/production/profile-photos/7260162/micro_images.jpg)
Chanuka Weerasinghe
2,337 PointsChallenge is expecting you to end the functions after - yay! - You need to get rid of one of the loops and that's it!
PS : Try using a "for" loop to reduce few lines.
Console.Write("Enter the number of times to print \"Yay!\": ");
var numOfTimes = System.Console.ReadLine();
int num = int.Parse(numOfTimes);
for (int i = 0; i < num; i++)
{
Console.WriteLine("\"Yay!\"");
}
Hope this teaches you something while fixing the error.
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 Points
Be careful about testing a challenge in workspaces or an external REPL.
If you have misunderstood the challenge, it's also very likely that you will misinterpret the results.
In this case, it looks like you have an outer loop that wasn't part of the challenge instructions and prevents the program from ending after it performs the function. For that matter, it looks like it prevents it from ever ending!
![Tristan Kasinger](https://secure.gravatar.com/avatar/164652cb3cd79104753944517b61f513?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Tristan Kasinger
919 PointsSo why does it run fine in the workshop? In the workshop, I can enter a number, get the right amount of yays, then type "quit" and it closes. It is only in the turn in screen that it suddenly has an exception.
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsIt "runs fine" in the workshop because you're allowing it to do something different that what the challenge asked for. The challenge is looking for a specific behavior, not just something that "runs".
But if you're asking why you get that specific error, it's not clear. I would guess that the challenge checking process is not programmed to handle all possible deviations from the expected results gracefully, and this particular deviation is causing it to crash instead of giving you a "Bummer!" message and potentially a helpful clue.
But you can still pass if you do what was asked. Re-read the instructions and you will not find anything about repeating until the user types "quit".