Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Final

Code compiles and run in another window, yet bombs out here with a big error code. Where's the issue?

I'm running one of your 'Finals' and it's bombing out for; System.ArgumentNullException: Value cannot be null. Parameter name: String at System.Number.StringToNumber (System.String str, NumberStyles options, System.NumberBuffer& number, System.Globalization.NumberFormatInfo info, Boolean parseDecimal) <0x7f6605fdea10 + 0x000ff> in <filename unknown>:0 at System.Number.ParseInt32 (System.String s, NumberStyles style, System.Globalization.NumberFormatInfo info) <0x7f6605fdd120 + 0x00094> in <filename unknown>:0 at System.Int32.Parse (System.String s) <0x7f6605fbe4a0 + 0x0001d> in <filename unknown>:0 at Treehouse.CodeChallenges.Program.Main () <0x4181dc50 + 0x00058> in <filename unknown>:0 at MonoTester.Run () [0x000d5] in /workdir/MonoTester.cs:93

The program runs in my workspaces window as requested. Program name is Test.cs.

I'd like to move on to the next section but if I'm not getting something, I'd like to be squared away first.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            while(true)
            {
                    Console.Write("Enter the number of times to print \"Yay!\": ");
                    string input = Console.ReadLine();                

                try 
                {
                    int count = int.Parse(input);

                    int i = 0;
                    while(i < count)
                    {
                        i += 1;   
                        Console.WriteLine("Yay!");
                    }
                }

                 catch(FormatException)
                 {
                    Console.WriteLine("You must enter a whole number.");
                    continue;
                 }  
            }     
        }
    }
}

2 Answers

Steven Parker
Steven Parker
212,135 Points

Your expectations of the functionality differ from the challenge. Code following the challenge instructions will ask for input once, and issue either a set of "Yay" messages, or an error message, but either way it will end.

This code has an added outer loop that prevents it from ending and causes it to ask for input again.

Ahh. I read too much into it.

Thanks!

Steven Parker
Steven Parker
212,135 Points

DAVID Grant — Glad to help. You can mark a question solved by choosing a "best answer".

And happy coding!