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

Fernando G. Azpiazu
Fernando G. Azpiazu
653 Points

I'm running it fine in the Workspace but it's not working in the Assessment: System.NullReferenceException error.

Hi! I was trying to complete the final step on the C# Basic course and I've written this code which is working in the Workspace without any problem, but when I try to "Preview" or "Recheck work" in the assessment, I get an error saying System.NullReferenceException.

Can anybody check it for me? I'm quite lost and it's the second code I'm getting this error with for the same exercise. I was trying it with a for instead of while for the loop and I've had the same situation: it was working in the Workspace but not in the Assessment window.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            while (true)
            {
                Console.Write("Enter the number of times to print \"Yay!\": ");
                var entry = Console.ReadLine();
                if (entry.ToLower() == "quit")
                {
                    Console.WriteLine("Goodbye!");
                    break;
                }
                else
                {
                  try
                  {
                    int count = int.Parse(entry);
                    if (count <= 0)
                    {
                      Console.WriteLine("You must enter a positive number");
                    }
                    else if (count > 0)
                    {
                    int n = 1;
                    while (n <= count)
                      {
                          Console.WriteLine("Yay!");
                          n++;
                      }
                    }
                  }
                  catch (FormatException)
                  {
                    Console.WriteLine("You must enter a valid number");
                  }
                }
            }
        }
    }
}
Fernando G. Azpiazu
Fernando G. Azpiazu
653 Points

Nevermind, I've fixed it re-writing it in a simpler way.

1 Answer

Steven Parker
Steven Parker
231,236 Points

It's always a good idea to stick with the challenge instructions.

Looks like you figured out that the extra additions were a problem. People often over-think this one and get too fancy.

The secret to challenge success is to do everything the challenge asks for, but nothing extra.