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 trialTristan 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
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
231,269 PointsBe 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
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
231,269 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".