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

Shing wai Chan
Shing wai Chan
1,255 Points

I don't know what cause the exception i get

i get System.NullReferenceException: Object reference not set to an instance of an object. but i can't finde it. i have commet some code out, but they make no difference.

Program.cs
using System;
namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
          int total = 0;
          int counter = 0;          
          while (true)
          {
            Console.Write("Enter the number of times to print \"Yay!\": ");            
            var user = Console.ReadLine();
            if ( user.ToLower()== "quit")
            { break;}

            // try
            //{
              int numberOfTime = int.Parse(user);            
              while( counter < numberOfTime)
              {
                Console.Write("\"Yay!\": ");
                counter++;
              } 
               counter = 0;
               total += numberOfTime;
              Console.WriteLine("\n"+ "Numbers of Yay enter:"+ total);           
           // }
            //catch (FormatException)
            //{              
             // Console.WriteLine("This is not a number");
             //continue;
            //}   
          }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,248 Points

Here are a few hints for passing this challenge:

  • Your program must ask for and accept input ONE TIME ONLY (no loop around input)
  • 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. Your Parse must be inside a try block.
Shing wai Chan
Shing wai Chan
1,255 Points

ahh, i got it! thanks for the explanation :)