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

Sebastian Lozano
Sebastian Lozano
3,936 Points

NullReferenceException

i cant fix the NullReferenceException Error i tryed to use the try and catch method but it didnt work in the TreeHouse Workspaces the program runs just fine . in there i used the format exception in case the user just pressed enter and it didnt have a input and it worked, but in the quiz i cant make it to work.

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")
                {
                    break;
                }

               try
               {
                  var timesPrintYay = int.Parse(entry);

                 if (timesPrintYay <= 0)
                  {
                     Console.WriteLine("please enter a number grater than 0");
                  }

                 while (timesPrintYay > 0)
                 {
                   Console.WriteLine("Yay!");

                   timesPrintYay -= 1;

                   if (timesPrintYay <= 0)
                   {
                        break;
                   }
                 }
                }
                catch(NullReferenceException)
                {
                  Console.WriteLine("Please insert a Number");
                  continue;
                }
             }
         }
    }
}

1 Answer

Just a couple of case errors in your strings.

The code challenges are really picky about what you print. If that challenge wanted you to print out "Hello" and you only printed "hello" with a lowercase H then that would cause an error in the challenge.

Things to fix listed here:

  1. Change "Please insert a Number" with "Please insert a number".
  2. Change "please enter a number greater than 0" to "Please enter a number than 0".

Good luck! ~alex